0

I am currently stuck with the following error:

IOError: [Errno 2] No such file or directory: '/home/pi/V1.9/Storage/Logs/Date_2018-08-02_12:51.txt'

My code to open the file is the following:

nameDir = os.path.join('/home/pi/V1.9/Storage/Logs', "Date_" + now.strftime("%Y-%m-%d_%H:%M") + ".txt")
f = open(nameDir, 'a')

I am trying to save a file to a certain path, which is /home/pi/V1.9/Storage/Logs. I am not sure why it can't find it, since I have already created the folder Logs in that space. The only thing being created is the text file. I am not sure if its suppose to join up like that, but I generally tried to follow the stages on this thread: Telling Python to save a .txt file to a certain directory on Windows and Mac

16
  • open(nameDir, 'w') Commented Aug 2, 2018 at 12:02
  • 1
    @Rakesh should it matter? It says that append will also create the file, if the file is not there. Commented Aug 2, 2018 at 12:04
  • @Lukali: The docs says a is for appending (docs.python.org/3.7/tutorial/inputoutput.html). What source are you refering to by 'It says'? Commented Aug 2, 2018 at 12:06
  • 1
    BTW, it's not a good idea to put ':' in file names. It's ok on *nix systems, but if you want to send that file to a Windows system, or even just write it to a Windows filesystem (NTFS or one of the FAT variants, which are still commonly used on USB sticks) on your own machine, it will cause problems. Commented Aug 2, 2018 at 12:20
  • 2
    Yes Lukali, a should work and create the file if it doesn't exist, just check the path is right try this in the terminal touch /home/pi/V1.9/Storage/Logs/Date_2018-08-02_12:51.txt and tell me the output Commented Aug 2, 2018 at 12:22

3 Answers 3

0

The problem seems to be here:

f = open(nameDir, 'a')

'a' stands for append, which means: the file should already exist, you get an error message because it doesn't. Use 'w' (write) instead, Python will create the file in that case.

Sign up to request clarification or add additional context in comments.

Comments

0

If you are creating the file use the write mode w or use a+

f = open(nameDir, 'w')
f = open(nameDir, 'a+')

Use only a append if the file already exist.

Comments

0

Not really an answer to your question, but similar error. I had:

with open("/Users//jacobivanov/Desktop/NITL/Data Analysis/Completed Regressions/{0} Temperature Regression Parameters.txt".format(data_filename), mode = 'w+') as output:

Because data_filename was in reality a global file path, it concatenated and looked for a non-existent directory. If you are getting this error and are referring to the file path of an external file in the name of the generated file, check to verify it isn't doing this.

Might help someone.

1 Comment

This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.