3
with open('C:/Users/JDoe/Downloads/rows_to_del.txt', 'r', encoding="utf8") as f:
delete_docs = [line.rstrip('\n') for line in f]
print(delete_docs)


def txtUpdater():
path = "C:/Users/JDoe/Downloads/TEST2/*.txt"
files = glob.glob(path)
for name in files:
    with open(name, 'r', encoding="utf8") as originaltxt, open("C:/Users/JDoe/Downloads/Output/Output.txt", 'w', encoding="utf8") as updatedtxt:
        for line in originaltxt:
            if not any(delete_doc in line for delete_doc in delete_docs):
                updatedDAT.write(line)

In the "path" above, the TEST2 folder has multiple sub-folders. Within each of those, there are text files. How do I make Python access all the text files inside the multiple sub-folders inside TEST2 and do what I am trying to do in the code above?

I tried putting a wildcard after TEST2 ("/*") in the path, but then the code did nothing.

path = "C:/Users/JDoe/Downloads/TEST2/*/*.txt"
Pika Supports Ukraine
  • 3,612
  • 10
  • 26
  • 42
sherrific
  • 31
  • 2
  • Use `**` with `recursive=True`, [as stated in the docs](https://docs.python.org/3/library/glob.html#glob.glob). – ekhumoro Mar 09 '19 at 00:35

0 Answers0