0

I was having issues writing to file, I've tried a lot of different methods of writing to file and none have worked. I was trying to figure out what was causing it so have used f.open, write, close to narrow down where the issue is coming from. I've narrowed it down to the pd.read_excel file here:

import glob
import os

# Open most recent file (probably to be changed)
list_of_files = glob.glob(r'C:\Users\gamo0\Downloads\*') # * means all if need specific format then *.csv
latest_file = max(list_of_files, key=os.path.getctime)

f = open("demofile1.txt", "a")
f.write("Now the file has more content!")
f.close()
df = pd.read_excel(latest_file,sheet_name = 'People in your team',header=8)

f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()

It successfully writes to demofile1.txt and closes it. But then fails on writing to demofile2.txt with the following error:

    f = open("demofile2.txt", "a")
FileNotFoundError: [Errno 2] No such file or directory: 'demofile2.txt'

A lot of similar issues talk about using absolute paths rather than relative, but it doesnt appear to be an issue with that as it can write to demofile1.txt fine.

N.B. if I move both write to demofiles above the pd.read_excel line, it will successfully write to both.

Any help would be appreciated.

Gary
  • 3
  • 1
  • Does the file `demofile2.txt` already exist in the folder? – Nick Feb 18 '22 at 14:18
  • Does this answer your question? [Writing to a new file if it doesn't exist, and appending to a file if it does](https://stackoverflow.com/questions/20432912/writing-to-a-new-file-if-it-doesnt-exist-and-appending-to-a-file-if-it-does) – AlexisG Feb 18 '22 at 14:19
  • @Nick When I run the script I delete both demofiles. If i run the code with the both the demofiles existing. It fails on the first f.close() with "[Errno 9] Bad file descriptor" – Gary Feb 18 '22 at 14:22
  • @AlexisG Unfortunately it doesnt help. I just tried using 'a+' and 'w'. Still fails with the same error – Gary Feb 18 '22 at 14:24

1 Answers1

0

I think it may be to do with how you are trying to write to the file. Try changing your open and write lines from:

 f = open("demofile1.txt", "a")
 f.write("Now the file has more content!")
 f.close()

to

with open("demofile1.txt", "a") as f:
    f.write("Now the file has more content!")

Using context managers is good practice when working with files. From the documentation here:

Context managers allow you to allocate and release resources precisely when you want to.

Nick
  • 3,454
  • 6
  • 33
  • 56
  • Just tried that and unfortunately still get the same error – Gary Feb 18 '22 at 14:58
  • Do you have any of the textfiles open as the code is running? – Nick Feb 18 '22 at 15:01
  • nope, all closed – Gary Feb 18 '22 at 15:01
  • and youre on windows? – Nick Feb 18 '22 at 15:01
  • yeah, on windows 10 (being run on an azure virtual desktop if it changes anything) – Gary Feb 18 '22 at 15:03
  • Can you check if you have write permission to that second textfile? compare the permissions on the two – Nick Feb 18 '22 at 15:04
  • neither file exists when running the script so theyre being created in the code above, so should be being created in the same way. I cant compare the permissions as the second textfile never gets created. The only reason I was opening them in append mode is because I copied code from further on in the script where the append is needed. – Gary Feb 18 '22 at 15:12