Filenotfounderror: [Errno 2] No Such File or Directory

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

FileNotFoundError means you are trying to open a file that does not exist in the specified directory (in this case, whatever directory you are running your Python script from).

the file "test_text.txt" is in the same folder as the program. it's a seperate folder from everything else that just has the text file and the program

In that case, you need to make sure that you're in the same directory as your file on your command-line, or specify the full path to test_text.txt (e.g. /home/user/Desktop/test_text.txt)

Python open() gives "FileNotFoundError: [Errno 2] No such file or directory:", but the file exists

You've specified an absolute path to the directory, r'C:\Users\Halid\Desktop\nlp_p5\nlp_dir2\TRAIN\posTr'. You then proceeded to list all files with their filenames, receiving A, B, C, ...

However, A, B, C, ... are not located in the directory you're running your python script.
Try calling

os.chdir(train_pos_path)

before calling the for loop.

Another solution would be to prepend each path with train_pos_path:

train_pos_path = r'C:\Users\Halid\Desktop\nlp_p5\nlp_dir2\TRAIN\posTr'

for filename in os.listdir(train_pos_path):
with open(os.path.join(train_pos_path, filename), "r",encoding="utf-8", errors='ignore') as f:
for line in f.readlines():
pos_lines.append(line)

The key takeaway from this is the fact that os.listdir(directory) returns just the names as seen from the directory, not their absolute paths.

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

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')

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

The issue is when you run file = open(filename, "rt"), it is looking for filename in the directory where you started Python (~/Dropbox/programming/first_project/), but you want it to read ~/Dropbox/programming/first_project/directoryaddress.

To ensure you reading the right file, you should either pass in the full path of it as filename or, if you know you will always find it in some subdirectory, simply prepend the path to filename before trying to read it file = open(files_path+"/"+filename, "rt") (there are cleaner ways to combine paths, like the standard library pathlib).



Related Topics



Leave a reply



Submit