21

I get a FileNotFoundError when trying to write to a file. This is my code:

def save_txt_individual_tracks(track, folder, i):
    f = open(folder+str(i)+'.txt','w+')
        for line in track:
            l=str(line[0])+','+str(line[1])+','+str(line[2])+'\n'
        f.write(l)
    f.close()

Which I call like so:

save_txt_individual_tracks(new,'E:/phoneTracks/'+track_name+'/',i)

Which gives me this error:

FileNotFoundError: [Errno 2] No such file or directory: 'E:/phoneTracks/TA92903URN7ff/0.txt'

I created folder phoneTracks in E. And I am confused about open() with mode 'w+', which is used to create a new file. Why do I get a FileNotFoundError? What can I do to fix it?

I am running python3.4.3 on windows 7

Neuron
  • 5,141
  • 5
  • 38
  • 59
CHEN
  • 223
  • 1
  • 2
  • 4
  • Hello and welcome to StackOverflow. Please take some time to read the help page, especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). And more importantly, please read [the Stack Overflow question checklist](http://meta.stackexchange.com/q/156810/204922). You might also want to learn about [Minimal, Complete, and Verifiable Examples](http://stackoverflow.com/help/mcve). – Morgan Thrapp Jul 14 '15 at 18:21
  • Thank you @poke for your help. And I will read these useful tips listed by Morgan Thrapp. – CHEN Jul 14 '15 at 20:10

1 Answers1

40

You are getting the error because the directory - E:/phoneTracks/TA92903URN7ff/ does not exist.

Example to show this error -

In [57]: open('blah/abcd.txt','w+')
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-57-46a07d4a5d18> in <module>()
----> 1 open('blah/abcd.txt','w+')

FileNotFoundError: [Errno 2] No such file or directory: 'blah/abcd.txt'

Got error in my code, because the directory blah/ does not exist.

If the directory - TA92903URN7ff/ is constant, try creating it and then running. If its not constant, you can checkout os.path.exists to check if the directory exists or not, and if it doesn't exist, create one using os.mkdir .

Example -

import os, os.path
def save_txt_individual_tracks(track,folder,i):
    if not os.path.exists(folder):
         os.mkdir(folder)
    elif not os.path.isdir(folder):
         return #you may want to throw some error or so.
    f = open(os.path.join(folder, str(i)+'.txt'),'w+')
        for line in track:
            l=str(line[0])+','+str(line[1])+','+str(line[2])+'\n'
        f.write(l)
    f.close()

Also, you should consider using os.path.join to join paths, instead of using string concatenation. And also using with statement for openning files as - with open(os.path.join(folder, str(i)+'.txt'),'w+') as f: , that way the file will automatically get closed once the with block ends.

Neuron
  • 5,141
  • 5
  • 38
  • 59
Anand S Kumar
  • 88,551
  • 18
  • 188
  • 176
  • Thank you very much @Anand S Kumar. The directory- 'TA92903URN7ff' is constant. I fixed the problem after using 'os.path.join'. By the way, I change 'foldermstr(i)' to 'folder+str(i)'. – CHEN Jul 14 '15 at 20:04