Exclude Some Lines By Keywords While Parsing Txt File
Im trying to parse txt files which contains lists of directories and files in it. Im intrested in '/ACS/SDU_:' and '/ACS/ScienceDataFile:' directories. How I can exclude such dirs
Solution 1:
exclude such dirs as /data/foo/bar/ATB6/Science/TGO/ACS: and /data/foo/bar/ATB7B/Science/TGO/ACS:?
Your if line.endswith
check will work only on the times you see that line. The condition itself is therefore evaluated at the wrong time as you're parsing the file (before you see the files for that path you are interested in).
You need to check the path
instead and store the suffixes you are interested in (use this each time you "save" the path
in the dict)
if any(path.endswith(x) for x in ['/ACS/SDU_:', '/ATB6/Science/TGO/ACS/ScienceDataFile:']):
filesDict[path] = files
files = []
Change the endswith(':')
back, as this will correctly identify all paths, not only those you are interested in. Feel free to extract the ['ACS/SDU_:', '/ACS/ScienceDataFile:']
list to its own variable for re-use
Post a Comment for "Exclude Some Lines By Keywords While Parsing Txt File"