Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python using open (w+) FileNotFoundError

Tags:

python

  1. Creating function saveTxtIndividualTracks(track,folder,i).Based on python3.4.3 and windows 7:

    def saveTxtIndividualTracks(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()
    
  2. Using the function:

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

When I ran the code, I got 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 open() function with mode 'w+', which is used to creat a new file. Why do I get a FileNotFoundError? What can I do to fix it?

like image 752
CHEN Avatar asked Jul 14 '15 18:07

CHEN


People also ask

How do I resolve FileNotFoundError in Python?

The solution to this problem is specifying the whole file path in the code. Note: Observe, while specifying the file path, we added an r before writing the path, pd. read_csv(r"C:\.......) . It is used to convert simple strings to raw strings.

How do I fix FileNotFoundError No such file or directory?

The Python "FileNotFoundError: [Errno 2] No such file or directory" occurs when we try to open a file that doesn't exist in the specified location. To solve the error, move the file to the directory where the Python script is located if using a local path, or use an absolute path.

Why Python Cannot find my file?

The Python FileNotFoundError: [Errno 2] No such file or directory error is often raised by the os library. This error tells you that you are trying to access a file or folder that does not exist. To fix this error, check that you are referring to the right file or folder in your program.

How do I fix file Not Found error?

In the above code, you are not giving the full path to a file to the open() function, just its name - a relative path. The error "FileNotFoundError: [Errno 2] No such file or directory" is telling you that there is no file of that name in the working directory. So, try using the exact, or absolute path.


1 Answers

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 saveTxtIndividualTracks(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.

like image 124
Anand S Kumar Avatar answered Oct 12 '22 13:10

Anand S Kumar