Why does not Atom recognize other files in the same directory? - python

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

Related

opening a .txt file

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

open with not creating and writing file

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

FileNotFoundError in Python Visul Studio

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.

how do i get current working directory in atom

In atom, os.getcwd() always returns D:\WorkSpace\Test. So if I do things like open("01.txt"), it cannot find the file.
Also, this happens when using the "script" package to execute in Atom, But when executing the actual python file, it works.
I have found several other asking the same question, like this, but there is still no resolution.
Thanks to everyone who tried to help!
Added my directory:
D:\WorkSpace\Test
D:\WorkSpace\Test\01\01.py
D:\WorkSpace\Test\01\01.txt
or
D:\WorkSpace\Test
└─01
└─ 01.py
└─ 01.txt
Added my source:
01.py
import os
print os.getcwd()
f = open("01.txt")
print f.read()
01.txt
atom editor 01.txt
Added results(in atom):
D:\WorkSpace\Test
Traceback (most recent call last):
File "D:\WorkSpace\Test\01\01.py", line 5, in <module>
f = open("01.txt")
IOError: [Errno 2] No such file or directory: '01.txt'
Added results(in windows cmd):
D:\WorkSpace\Test\01>01.py
D:\WorkSpace\Test\01
atom editor 01.txt
In Mac, I solved this kind of issue by opening ATOM from a shell window, under target directory. Seems ATOM will use the directory it inherited from shell process as its working directory. You might try with a windows cmd window, see if it works.
Windows - go to Packages -> Settings View -> Manage Packages. Then go to System Settings on the left hand side menu and tick 'Show in file context menus'.
You can now go to your chosen directory and open any file (.js, .py etc.) with Atom and the current working directory will change to the one you chose instead of the default .atom.

Full File Path vs. Short OSX

I'm running OSX Mavericks but this problem has been going on since I had Snow Leopard.
When writing a script in any language, eg: Python. When I try to open a file the short
form doesn't work.
file = open('donkey.jpg')
And I get this error:
IOError: [Errno 2] No such file or directory: 'donkey.jpg'
Instead, I always have to specify the full path.
file = open('/Users/myName/Desktop/donkey.jpg')
Any ideas on why this could be happening and how to correct it?
If you specify donkey.png, it means donkey.jpg file in the current working directory. (relative path)
Make sure you're running the script in the same directory where donkey.jpg exists.
If you want specify the image file path relative to the script file instead of current working directory use following:
import os
filepath = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'donkey.jpg')
NOTE You can use __file__ only in script file. (not in interactive mode)
Your open call does not have a mode parameter. In which case, it defaults to opening the file in read mode.
Unless the file you are opening (to read) resides in the current working directory, it is completely expected that the python script throws a IOError.

Categories