The below should do; code demos of what each annotated line does afterwards:
path = "/Users/xxx/Data/"
allFiles = [os.path.join(path, filename) for filename in os.listdir(path)] # [1]
del_keystring = "DT=(SINGLE SINGLE SINGLE)" # general case
for filepath in allFiles: # better longer var names for clarity
print(filepath)
with open(filepath,'r') as f_read: # [2]
loaded_txt = f_read.readlines()
new_txt = []
for line in loaded_txt:
if del_keystring not in line:
new_txt.append(line)
with open(filepath,'w') as f_write: # [2]
f_write.write(''.join([line for line in new_txt])) # [4]
with open(filepath,'r') as f_read: # [5]
assert(len(f_read.readlines()) <= len(loaded_txt))
- 1
os.listdir
returns only the filenames, not the filepaths; os.path.join
joins its inputs into a fullpath, with separators (e.g. \\
): folderpath + '\\' + filename
- [2] NOT same as doing
with open(X,'r') as .., with open(X,'w') as ..:
; the as 'w'
empties the file, thus nothing for as 'r'
to read
- [3] If
f_read.read() == "Abc\nDe\n12"
, then f_read.read().split('\n')==["Abc,"De","12"]
- [4] Undoes [3]: if
_ls==["a","bc","12"]
, then "\n".join([x for x in _ls])=="a\nbc\n12"
- [5] Optional code to verify that saved file's # of lines is
<=
original file's
- NOTE: you may see the saved filesize slightly bigger than original's, which may be due to original's better packing, compression, etc - which you can figure from its docs; [5] ensures it isn't due to more lines
# bonus code to explicitly verify intended lines were deleted
with open(original_file_path,'r') as txt:
print(''.join(txt.readlines()[:80])) # select small excerpt
with open(processed_file_path,'r') as txt:
print(''.join(txt.readlines()[:80])) # select small excerpt
# ''.join() since .readlines() returns a list, delimited by \n
NOTE: for more advanced caveats, see comments below answer; for a more compact alternative, see
Torxed's version