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')
    – Rakesh
    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.
    – Lukali
    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'?
    – Sheldore
    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.
    – PM 2Ring
    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
    – aldokkani
    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.

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.

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.