Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
file Problem
#3
It's possible that your file was not written where you expected it was going to be written.
That can be assured if you set the starting directory, or specify a full path (as below). The statement shown here
guarantees that the starting directory is the same as that of the script.
Also, using 'with' on your open statement will automatically close the file at end and is the preferred method to use:

import os

os.chdir(os.path.abspath(os.path.dirname(__file__)))
with open("test.txt","w") as test:
    test.write('test hello')
faraz_h likes this post
Reply


Messages In This Thread
file Problem - by faraz_h - Apr-12-2021, 06:51 AM
RE: file Problem - by ibreeden - Apr-12-2021, 07:40 AM
RE: file Problem - by Larz60+ - Apr-12-2021, 09:47 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020