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('.')
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 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 have a small enough Python project in Eclipse Neon and keep getting the same error and can't find proper documentation on how to solve. In my main I need to call a file that is located in another folder. The error I receive is IOError: [Errno 2] No such file or directory:
I have an empty init.py file in the folder (XML_TXT) that I'm trying to use.
It looks like Groovy is importing okay, or else you would get an ImportError. An IOError indicates that it can't find "test.txt". Does that file exist?
It will work if the file path is relative to where you are running the script from. So for example if test.txt is in a folder
Groovy("folder_name/test.txt")
You can also go up in the directory structure if you need to, for example
Groovy("../folder_name/test.txt")
Or, if you want to be able to run the file from anywhere, you can have python work out the absolute path of the file for you.
import os
filename = os.path.join(os.path.dirname(__file__), 'folder_name/test.txt')
u = Groovy(filename)
I want to use some text files that I have in the same directory in my script. But Atom always gives me this error
FileNotFoundError: [Errno 2] No such file or directory: 'comments.txt'
[Finished in 0.156s]
Anyone know how to fix this?
This is my code
with open('comments.txt', 'r') as f:
myNames = [line.strip() for line in f]
print(myNames)
It works on other IDE's such as Pycharm and sublime text. I also tried it on the python idle.
In Atom, it only works when I give it the full path. But I want it to only use the project path.
If you are using the Script package, go to File>Settings (or CTRL+,) then open the Packages tab. Find script and open its Settings menu. The first setting is "Change Current Working Directory (CWD) Behavior." Change the value from "First project directory" to "Directory of script."
It could be that atom changes a file path.
Run a file or command to check
import os
print(os.getcwd())
This will tell you which dir you are working from.
Use this in your script to change dir
os.chdir(PATH_TO_DIR)
or write full file path
I am getting the above error when I use a webserver to run my code, however locally in the Terminal this works fine. I believe this must be to do with the path to the file working locally but not remotely. I have seen the solution on stackoverflow is to add the filepath like '/user/xxx/library/' etc, however is there a solution that allows this to be system agnostic? As in if I copy this directory to another location/server it will still work?
You can import os, it's built it to Python. You can get teh absolute path of the .py file this way:
import os
ROOT = lambda base : os.path.join(os.path.dirname(__file__), base).replace('\\','/')
Now you can simply do the following:
ROOT('users.txt')
It should return the absolute path.