I'm currently doing an assignment that requires me to use CSV files in Python. I have the .csv file in the same directory as the program itself, yet it gives me the same "No such file or directory" error.
I am sure the name is not a problem. This problem has happened to me before with another file and I wasn't able to fix that either.
Code:
with open('file.csv') as file:
content = csv.reader(file, delimiter=',')
FileNotFoundError: [Errno 2] No such file or directory: 'file.csv'
I fixed this by using the OS library for python.
Change the current working directory with:
os.chdir(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe()))))
This changes Python's working directory to the one that your file is contained in.
Related
first, i'd like to mention that im using python via visual studio. not sure if this information will
be relevant but this is my first time using file input so i'm not sure
basically, i have a .txt file located in the same location as my .py file. however, when i go to access it, i get an error 'FileNotFoundError: [Errno 2] No such file or directory'
is there a way to make it work or a different IDE i should use?
I tried reading a .txt file from the same location as my .py file. however, i keep getting the error 'FileNotFoundError: [Errno 2] No such file or directory'
Take into account that the script might not be running from the same path where your python script is and most probably your are not specifying the exact path of the file.
If your file is located in the same directory where your python sript is you can use the pathlib library this way to get your script to work:
import pathlib
# Some custom code
# find your current script path
current_script_path = pathlib.Path(__file__).parent
my_file_name = "my_file_name.txt"
# Specify the file path relative to your script's path
my_file_path = current_script_path / my_file_name
with open(my_file_path, "r+") as f:
print(f.read())
I am following a tutorial (Beautiful Soup) and I can't seem to get open with to create a .txt file as I am getting this error:
with open(f'posts/{index}.txt', 'w+') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'posts/1.txt'. I tried looking up different solutions to my problem currently running python 3.9. Is there another step I need to correct this issue?
The interpreter isn't finding the directory "posts" in your current working directory. If it exists, you are not in its parent directory. Try using the full path, e.g. rf'C:\users\myuser\posts\{index}.txt' or rf'~/posts/{index}.txt' on linux.
Or, if it doesnt't exist, add these lines:
import os
os.mkdir('posts')
You can find your current working directory this way:
import os
os.path.abspath('.')
I get this when I try to run my python project code
with open("config.json", "r") as f:
Exception raised:
FileNotFoundError: [Errno 2] No such file or directory: 'config.json'
what is a solution for this?
here is a screenshot of the code and everything.
the file config.jason needs to be in your current working directory to be found by open if you do not use a full path. Your cwd is probably where you launched python. To check do
import os
print(os.getcwd())
Your easiest solution is probably to use a full path name for the file.
As shown in the screenshot of the IDE, you should exactly point out where the .json file. Now the json file and python code are not located in the same folder.
FYI, there are two types of path, absolute path and relative path. Please see https://www.kite.com/python/answers/how-to-find-the-absolute-and-relative-path-of-a-file-in-pythonhttps://www.kite.com/python/answers/how-to-find-the-absolute-and-relative-path-of-a-file-in-python
with open("/file program/config.json", "r") as f
content = f.read()
# do something
Or, just move out the config.json file to the folder, which the python code is located.
I know there is a lot of No such file or directory error. But I think this one is different.
It's easy to understand when the file was not existed and opened with normal mode read or write, or the intermediate directory is not existed.
But I got this when using append mode and the filename is a relative filename without / in the middle (it contains unicode characters).
I got the error from log file and I cannot reproduce it with same filename.
FileNotFoundError: [Errno 2] No such file or directory: 'A01_UE需求设计_V1_1_20211013160004'
... other call stack
File "pkg/xxx.py", line 304, in download
with open(file_name, 'ab') as f:
Other info:
os: ubuntu 16.04
kernel: 4.4.0-116-generic
python: version 3.8
filesystem: ext4 on lvm
Problem resolved.
I struggled with the open(f, 'ab') but never found any problem.
I have a sudden idea this morning. Maybe someone changed cwd. And I searched in the project and found it. Someone wrote code like following
with tempfile.TemporaryDirectory() as tmpdir:
os.chdir(tmpdir)
This code changed the working dir and did not restore it. The tmpdir got removed since it was created in with context.
And I cannot reproduce it because above code is in handler of another api request.
I am having issues with the open() function in Python 3.2.3. The following code works well using 2.7.3, but not with Python 3:
file = open("text.txt", 'r')
In Python3, it gives me a standard IOError:
IOError: [Errno 2] No such file or directory: 'text.txt'
Note that the text.txt file that is referenced is in the same directory as the python file.
Any ideas?
The file name is not relative to the directory of the file, but your current working directory (which you can find out with os.getcwd()).
If you want to open a file whose name is relative to your Python file, you can use the magic variable __file__, like this:
import os.path
fn = os.path.join(os.path.dirname(__file__), 'text.txt')
with open(fn, 'r') as file:
# Do something, like ...
print(file.read())
You are trying to open a file in read mode, and this file has to exist.
Perhaps problem is that the file just doesn't exist in your python3 path and therefore the open command fails, but 'text.txt' exist on your python2.7 lib (or somewhere in python2.7 path) and this is why python is able to locate the file and open it.
You can just try this code (this will assure you the file exist since you create it):
f = open('text.txt','w')
f.close()
f.open('text.txt','r')
I was using Eclipse with Pydev, and had the text.txt file within the package instead of on the project level. To access a file within the package, you need to use:
file = open("[package]/text.txt", 'r')