1

I'm trying to edit a file outside my Python directory(edit files inside Python directory works). my Python directory is in c:\Users\test\pycharmproject\pythonproject. while debugging, the code can see all the files that end with .txt. in my case, I want to edit test.txt.but the code does not add the string to it. the code ends without any errors. The code:

def foo(folderPath, fileEx, text):
    for file in os.listdir(folderPath):
       if file.endswith(fileEx):
          f=open(file, "a")
          f.write(text)
          f.close()
          print(folderPath, fileEx)#c:\Users\test, text.txt

foo("c:\\Users\\test", ".txt", "-----")

The code needs to search all the files that end with ".txt" and add to it some string.

While I'm trying to do that without the Function I can edit that file that is outside my python directory. for example:

f=open("c:\\Users\\test\\file.txt", "a")
f.write("----")
f.close()
jonny
  • 25
  • 8
  • 4
    You need to include the folderPath when opening the file `open(f"{folderPath}/{file}", a)` –  Mar 05 '22 at 09:18

0 Answers0