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

Lukali
  • 343
  • 3
  • 15
  • `open(nameDir, 'w')` – Rakesh Aug 02 '18 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 02 '18 at 12:04
  • @Lukali: The docs says `a` is for appending (https://docs.python.org/3.7/tutorial/inputoutput.html). What source are you refering to by 'It says'? – Sheldore Aug 02 '18 at 12:06
  • Possible duplicate of [open() in Python does not create a file if it doesn't exist](https://stackoverflow.com/questions/2967194/open-in-python-does-not-create-a-file-if-it-doesnt-exist) – Abhi Aug 02 '18 at 12:09
  • Yes, opening the file in `'a'` mode should create it if it doesn't exist. See https://stackoverflow.com/a/23566951/4014959 – PM 2Ring Aug 02 '18 at 12:09
  • I can't use 'w' because I am planning to open the file more than once. I need to create a file with a certain date, and then output the whole log from that date to the file. Also I have now tried a+. I am actually getting the same error? – Lukali Aug 02 '18 at 12:20
  • 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 02 '18 at 12:20
  • @PM2Ring you're right. Ive noticed it a bit ago and have already made the change, so it doesn't seem to be that. I changed it to '_' – Lukali Aug 02 '18 at 12:21
  • As I said, using `'a'` mode isn't the cause of your problem, despite what the answers below claim. Are you _certain_ that the directory does actually exist? – PM 2Ring Aug 02 '18 at 12:22
  • 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 02 '18 at 12:22
  • @HossamAhmedAl-Dokkani Very weird situation. I actually do the following in command line: git clone https://github.com/Ritzerk/V1.9.git and then touch /home/pi/V1.9/Storage/Logs just to get not a directory error. If you do look on the github, the folder is present there. – Lukali Aug 02 '18 at 12:42
  • tell me the output of `echo $PWD` where you run `git clone` command – aldokkani Aug 02 '18 at 12:45
  • @HossamAhmedAl-Dokkani I get no message when I do touch /home/pi/V1.9/storage/Logs and touch /home/pi/V1.9/Storage . It's odd. I will try changing the capital on the storage it might make all the difference just like it did in the command prompt. – Lukali Aug 02 '18 at 12:45
  • @Lukali there problem here is dir tree when you run 'touch /home/pi/V1.9/storage/Logs` you create a file(not a dir) named 'Logs' just run `echo $PWD` where you do git clone and tell me the output – aldokkani Aug 02 '18 at 12:48
  • @HossamAhmedAl-Dokkani Thank you hossam I am no longer getting the error. I had to change /home/pi/V1.9/Storage/Logs/Date... to /home/pi/V1.9/storage/Logs/Date... . Anyway to make you the answer to this post? – Lukali Aug 02 '18 at 12:53
  • @Lukali you are welcome, I delete my answer since it was irrelevant. so no worries glad that your problem is solved. – aldokkani Aug 02 '18 at 12:56

2 Answers2

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.

Rakesh
  • 78,594
  • 17
  • 67
  • 103
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.

vgratian
  • 43
  • 9