Python error: FileNotFoundError: [Errno 2] No such file or directory

94,764

Solution 1

You are not giving the full path to a file to the open(), just its name - a relative path.

Non-absolute paths specify locations in relation to current working directory (CWD, see os.getcwd).

You would have to either os.path.join() correct directory path to it, or os.chdir() to the directory that the files reside in.

Also, remember that os.path.abspath() can't deduce the full path to a file just by it's name. It will only prefix its input with the path of the current working directory, if the given path is relative.

Looks like you are forgetting to modify the the file_array list. To fix this, change the first loop to this:

file_array = [os.path.join(prefix_path, name) for name in file_array]

Let me reiterate.

This line in your code:

file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if f.endswith('.txt')]

is wrong. It will not give you a list with correct absolute paths. What you should've done is:

import os
import glob

prefix_path = ("C:/Users/mpotd/Documents/GitHub/Python-Sample-"    
               "codes/Mayur_Python_code/Question/wx_data/")
target_path = open('MissingPrcpData.txt', 'w')
file_array = [f for f in os.listdir(prefix_path) if f.endswith('.txt')]
file_array.sort() # file is sorted list

file_array = [os.path.join(prefix_path, name) for name in file_array]

for filename in file_array:
     log = open(filename, 'r')

Solution 2

You are using relative path where you should be using an absolute one. It's a good idea to use os.path to work with file paths. Easy fix for your code is:

prefix = os.path.abspath(prefix_path) 
file_list = [os.path.join(prefix, f) for f in os.listdir(prefix) if f.endswith('.txt')]

Note that there are some other issues with your code:

  1. In python you can do for thing in things. You did for thing in range(len(things)) it's much less readable and unnecessary.

  2. You should use context managers when you open a file. Read more here.

Share:
94,764
Mayur Potdar
Author by

Mayur Potdar

Updated on April 12, 2021

Comments

  • Mayur Potdar
    Mayur Potdar about 3 years

    I am trying to open the file from folder and read it but it's not locating it. I am using Python3

    Here is my code:

    import os
    import glob
    
    prefix_path = "C:/Users/mpotd/Documents/GitHub/Python-Sample-                
    codes/Mayur_Python_code/Question/wx_data/"
    target_path = open('MissingPrcpData.txt', 'w')
    file_array = [os.path.abspath(f) for f in os.listdir(prefix_path) if 
    f.endswith('.txt')]
    file_array.sort() # file is sorted list
    
    for f_obj in range(len(file_array)):
         file = os.path.abspath(file_array[f_obj])
         join_file = os.path.join(prefix_path, file) #whole file path
    
    for filename in file_array:
         log = open(filename, 'r')#<---- Error is here
    

    Error: FileNotFoundError: [Errno 2] No such file or directory: 'USC00110072.txt'

  • Mayur Potdar
    Mayur Potdar about 6 years
    1. for your first note: it's helping me to get the location of each file in the folder. 2. I have used your suggestion and still, it's showing me an error.
  • CBLT
    CBLT over 2 years
    I have the same issues. I am using ciscoconfparse module in Python to read and parse several cisco config files. It worked for the first 10 config files in a folder okay. When I added additional configs into the same folder, it gave me the [FATAL] ciscoconfparse could not open error for the new configs I added.