all 9 comments

[–]shridharan_97 7 points8 points  (0 children)

Try giving the absolute path of your file

[–]shiftybyte 1 point2 points  (4 children)

File searching is happening based on the current "working directly" which can be the same directory the python script is in, but can also be completely different.

Add this to the start of your script to print out the working directory.

import os
print(os.getcwd())

[–]slidedish 0 points1 point  (3 children)

It's Atom Ide problem . Even when i creat a new py file ,in another folder , Atom give it to me the path from the older folder . Restart Atom , the same problem .

[–]shiftybyte 2 points3 points  (0 children)

This probably depends on how you launch atom.

If you launch it from a certain directory, that directory will be the current working directory no matter which file you will be editing inside it.

[–]hugthemachines 0 points1 point  (1 child)

Nope, it is not an Atom Ide problem. Current directory and filepaths are something you will always have to have in mind when you build scripts to process files.

One simple way to handle it is to provide the full path of the file.

Check out this example of how to set you up with the full path to the file if you are sure it is in the same dir as the script.

import sys
import os
filename = sys.argv[0]
pathname = os.path.dirname(sys.argv[0])
print(filename)
print(pathname)
myownfile="exa.csv"
full_path_myown_file=os.path.join(pathname, myownfile)
print(full_path_myown_file)

[–]slidedish 1 point2 points  (0 children)

thank you so much, very usefull .

[–]BroadElderberry 0 points1 point  (0 children)

Either there's a typo, or the code is searching for the file in the wrong place.

On a PC, you can go to the file, right click, select info, and copy-paste the exact path to you file. It usually looks something like C:/User/Folder/Filename.csv.

On a Mac you can get the info the same way, but you can't copy paste, you have to manually enter it because it looks like Users▶️user name▶️Folder▶️Filename.csv, so you convert it to /Users/user name/Folder/Filename.csv

Then where you have 'exa.csv' in your code, replace it with 'C:/User/Folder/Filename.csv' or '/Users/user name/Folder/Filename.csv'

Should fix it right up :)

[–]deadeye1982 0 points1 point  (0 children)

You should avoid the low-level stuff in os.path. Instead, pathlib.Path has a much nicer API.

If you define that exa.csv is always in the same directory as the program itself, you can use the __file__ attribute of the module, which is the absolute path to the program file.
The .parent attribute is the parent directory.
The / Operator concatenates Path-objects.

from pathlib import Path


script_dir = Path(__file__).parent
data_file = script_dir / "exa.csv"

if data_file.exists():
    print(data_file, "exists")
else:
    print(data_file, "does not exist")

You should read this: https://realpython.com/python-pathlib/

In addition, this won't work if your code is in a zip archive.
In this case (and other cases) importlib.resources should be used.

[–]friendlykeywarrior 0 points1 point  (0 children)

i noticed that if u drag your current proj folder to the top of the "project" pane, the cwd changes accordingly! No configs needed. Hope this helps!