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

I am trying to open all the '*.json' files in a directory and get some data out of them
import json
import os #os module imported here
import glob
path = 'C:\Saba\Python Workspace'
for filename in os.listdir(path):
if filename.endswith('.json'):
with open(filename,'r') as f:
data = json.load(filename)
print(data['key'],end="*")
for path in data['paths']:
print(path['method'],end="*")
for resources in path['resources']:
print(resources['key'],end="*")
print("\b"+"$")
This is the Error i get :
Traceback (most recent call last):
File "c:/Saba/Python Workspace/folder.py", line 9, in <module>
with open(filename,'r') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'order-securityTransferOrders-service-v1.0.0-inventory.json'

You are running the script in different path. Adding the absolute path of the filename will do the trick.
import os.path
for filename in os.listdir(path):
abs_file_path = os.path.abspath(filename)
if filename.endswith('.json'):
with open(abs_file_path,'r') as f:
# your code ....

replace line with open(filename,'r') as f: with with open(os.path.abspath(filename),'r') as f:

Where do you run this script ? Python tries to search for your Json file in the current script folder.
If you want Python to find within the given path, you should write somthin like :
with open(os.path.join(path,filename) ,'r') as f:

Related

Why is this program "unable to find file or directory" when I am only trying to delete duplicate files?

I am trying to delete all duplicate files from my computer using a Python script
import os
import hashlib
# Set the directory you want to search for duplicate files
dir_path = "/Users/ebbyrandall"
# Create a dictionary to store the files and their corresponding hashes
files_dict = {}
# Walk through the directory and calculate the hash of each file
for root, dirs, files in os.walk(dir_path):
for file in files:
file_path = os.path.join(root, file)
with open(file_path, "rb") as f:
file_data = f.read()
file_hash = hashlib.md5(file_data).hexdigest()
# If the file hash is not in the dictionary, add it
# Otherwise, it's a duplicate and we should delete it
if file_hash not in files_dict:
files_dict[file_hash] = file_path
else:
os.remove(file_path)
I get the following error message:
Traceback (most recent call last):
File "deleteDuplicateFiles.py", line 14, in <module>
with open(file_path, "rb") as f:
IOError: [Errno 2] No such file or directory: '/Users/ebbyrandall/myPython/env/bin/python3'
Let's say you have this situation:
folder1/file.txt
And then run this:
import os
for folder in ['folder1/file.txt', 'folder1/other.txt', 'folder2/file.txt']:
try:
os.remove(folder)
print('file from {folder} removed')
except Exception as e:
print('problem removing file from folder{folder}:')
print(e)
The result is:
file from {folder} removed
problem removing file from folder{folder}:
[WinError 2] The system cannot find the file specified: 'folder1/other.txt'
problem removing file from folder{folder}:
[WinError 3] The system cannot find the path specified: 'folder2/file.txt'
This makes sense because in the first case, the file was there and could be removed. In the second case, the file didn't exist (error 2) and in the third case the folder didn't exist (error 3).
However, in your case, you were actually trying to open a folder as a file, or to open a file in a folder that doesn't exist:
try:
with open('folder', 'rb') as f:
print('File "folder" opened')
except Exception as e:
print('problem opening file "folder"')
print(e)
try:
with open('folder2/file.txt', 'rb') as f:
print('File "folder2/file.txt" opened')
except Exception as e:
print('problem opening file "folder2/file.txt"')
print(e)
Result:
problem opening file "folder"
[Errno 2] No such file or directory: 'folder'
problem opening file "folder2/file.txt"
[Errno 2] No such file or directory: 'folder2/file.txt'
The solution would be to distinguish files from folders and only to open files. Also, as the examples show, you can catch other errors (for example being unable to open a file that is locked by some other program) with a try .. except .. block.
Issues with symbolic links
Assume your file name /Users/ebbyrandall/myPython/env/bin/python3 is a symbolic link because folder myPython is a virtual environment created with venv.
Reproduce the error
Creating a virtual environment
python3 -m venv ./my_venv cd my_venv ls -l my_venv/bin/python3
shows a symbolic link: lrwxrwxrwx my_venv/bin/python3 -> /usr/bin/python3
Then trying to read from this file name:
import os
root = '/Users/hc_dev'
file = 'my_env/bin/python3'
file_path = os.path.join(root, file)
with open(file_path, "rb") as f:
file_data = f.read()
print(file_data)
outputs your error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: '/Users/hc_dev/my_env/bin/python3'
Debug and test the type and existence
According to
How to check if file is a symlink in Python?
this file can be evaluated with pathlib as follows:
is not a link
is not a symlink
does not exist
is not a regular file
os.path.islink(file_path)
# False
from pathlib import Path
Path(file_path).is_symlink()
# False
Path(file_path).exists()
# False
Path(file_path).is_file()
# False

Can't open file in the same directory

I was following a python tutorial about files and I couldn't open a text file while in the same directory as the python script. Any reason to this?
f = open("test.txt", "r")
print(f.name)
f.close()
Error message:
Traceback (most recent call last):
File "c:\Users\07gas\OneDrive\Documents\pyFileTest\ManipulatingFiles.py", line 1, in <module>
f = open("test.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
Here's a screenshot of proof of it being in the same directory:
The problem is "test.txt" is a relative file path and will be interpreted relative to whatever the current working directory (CWD) happens to be when the script is run. One simple solution is to use the predefined __file__ module attribute which is the pathname of the currently running script to obtain the (aka "parent") directory the script file is in and use that to obtain an absolute filepath the data file in the same folder.
You should also use the with statement to ensure the file gets closed automatically.
The code below shows how to do both of these things:
from pathlib import Path
filepath = Path(__file__).parent / "test.txt"
with open(filepath, "r") as f:
print(f.name)

read files in a directory specified by a command line argument

I am trying to read files from a directory specified by a command line argument.The command line:
./printfiles.py ./test_dir
The code I have so far is:
#!/usr/bin/env python3
import os
import sys
input_dir=argv[1]
for file in os.listdir(input_path):
with open(file, "r") as f:
for line in f.readlines():
print(line)
I am getting the error:
FileNotFoundError: [Errno 2] No such file or directory: 'hello.txt'
I think that the problem is because os.listdir() only returns file names, not the path. I am confused as how to do this because the path is just specified by the user.
I think that the problem is because os.listdir() only returns file names, not the path.
I think you're correct.
You can give open the full path to the file by using os.path.join:
with open(os.path.join(input_dir, file), "r") as f:

Why does do I have an IO error saying my file doesn't exist even though it does exist in the directory?

I am trying to loop over a Python directory, and I have a specific file that happens to be the last file in the directory such that I get an IOerror for that specific file.
The error I get is:
IOError: [Errno 2] No such file or directory: 'nod_gyro_instance_11_P_4.csv'
My script:
for filename in os.listdir("/Users/my_name/PycharmProjects/My_Project/Data/Nod/Gyro"):
data = []
if filename.endswith(".csv"):
data.append(k_fold(filename))
continue
else:
continue
k_fold does this:
def k_fold(myfile, myseed=11109, k=20):
# Load data
data = open(myfile).readlines()
The entire traceback:
Traceback (most recent call last):
File "/Users/my_name/PycharmProjects/MY_Project/Cross_validation.py", line 30, in <module>
data.append(k_fold(filename))
File "/Users/my_name/PycharmProjects/My_Project/Cross_validation.py", line 8, in k_fold
data = open(myfile).readlines()
IOError: [Errno 2] No such file or directory: 'nod_gyro_instance_11_P_4.csv'
My CSV files are such:
nod_gyro_instance_0_P_4.csv
nod_gyro_instance_0_P_3.csv
nod_gyro_instance_0_P_2.csv
nod_gyro_instance_0_P_5.csv
...
nod_gyro_instance_11_P_4.csv
nod_gyro_instance_10_P_6.csv
nod_gyro_instance_10_P_5.csv
nod_gyro_instance_10_P_4.csv
Why doesn't it recognize my nod_gyro_instance_10_P_4.csv file?
os.listdir returns just filenames, not absolute paths. If you're not currently in that same directory, trying to read the file will fail.
You need to join the dirname onto the filename returned:
data_dir = "/Users/my_name/PycharmProjects/My_Project/Data/Nod/Gyro"
for filename in os.listdir(data_dir):
k_fold(os.path.join(data_dir, filename))
Alternatively, you could use glob to do both the listing (with full paths) and extension filtering:
import glob
for filename in glob.glob("/Users/my_name/PycharmProjects/My_Project/Data/Nod/Gyro/*.csv"):
k_fold(filename)

I'm getting an IO ERROR for a file that is not in the path where I'm parsing files

I continuously get an error when I open all text and log files in a particular path and print the log that the match was found in. Below is the Error returned and the code. Does anyone know why I'm getting this error; the code works the way it's supposed to work? Thanks!
ERROR:
file: c:\Python27\13.00.log
Traceback (most recent call last):
File "C:\Python27\allfiles.py", line 20, in <module>
f=open(file, "r")
IOError: [Errno 2] No such file or directory: 'LICENSE-OpenSSL.txt'
CODE:
import os, sys
import string
userstring = 'Rozelle07'
path = 'c:\\Python27'
for root,dirname, files in os.walk(path):
for file in files:
if file.endswith(".log") or file.endswith(".txt"):
f=open(file, "r")
for line in f.readlines():
if userstring in line:
print "file: " + os.path.join(root,file)
break
f.close()
I think you need to open the file using its absolute path, not just its filename. Try changing your open(..) line to f = open(os.path.join(root, file) and it should work.
Edit: The following works for me (I'm also on Windows with Python 2.7):
#!/usr/bin/env python
import os
userstring = 'test'
path = 'c:\\Python27'
for root, dirname, files in os.walk(path):
for file in files:
if file.endswith(".log") or file.endswith(".txt"):
filepath = os.path.join(root, file)
with open(filepath, 'r') as f:
for line in f:
if userstring in line:
print "%s in %s" % (userstring, filepath)
break
else:
print "%s NOT in %s" % (userstring, filepath)
When you do a file open the variable 'file' doesn't contain the fullpath hence you get the error.

Categories

Resources