Error while trying to open CSV file in python - python

Python rookie here. Trying to use the CSV module, but I get an error:
IOError: [Errno 2] No such file or directory: 'books.csv'
The actual CSV file is stored on my desktop. I suppose this means Python can't find the file? If so, how can I fix this?
Many thanks in advance!

Since your file name includes no path component, it is implicitly assigned to be in the current directory. Unless your current directory is the desktop, you won't be able to find the file.
Either give it a full pathname, or move to the desktop and run your script from there.

Related

Can't find and read .csv file

I recently brought my code from Jupyter into VSCode. I fixed up some problems with my imports, but now I can't find a .csv file. The code and .csv file are saved in the same folder. My code is:
import pandas
df = pandas.read_csv('emailss.csv', names=['Email', 'Amiability'])
After running the program, it says,
FileNotFoundError: [Errno 2] No such file or directory: 'emailss.csv'
What's going on here? How can I fix it? It was working perfectly in Jupyter.
Here is a screenshot:
where are you running the code from? Is the CSV file in the same directory as your python file, if yes is the console path the same as the python script and the CSV file?
Check with os.getcwd() that your current working directory matches with the file path

Import a single file from another directory

I'm kind of new with python and there is something troubling me. I'm using pandas to read an excel file. All works well if I have the excel in the same directory as my .py file.
What I want to know is what is the best way to get a file that is in a completely different path. I've searched a lot and haven't found a straightforward answer. I've seen examples with sys.append, examples with external libraries, etc. I am trying to understand the pros/cons as well of each solution. Ideally I would like to have the file path as a user input
sys.path is where Python tries to locate modules or packages to import them. Not files when you are trying to access them. (like your excel file)
On the other hand, open built-in function or Pandas read functions require a path to a file. As long as you work with absolute paths you don't need to worry about it.
If you want to give it a relative path, first check to see where you are and then locate your file relative to your working directory.
with os.getcwd() you can get your current working directory.
As an example, suppose we have:
tmp/
└── inside_directory
├── pfile.py
└── text.txt
If my terminal indicates that I'm in tmp directory and I run my pfile.py with:
python inside_directory/pfile.py
and the content of pfile.py be:
with open("text.txt") as f:
print(f.read())
I will get error which says: FileNotFoundError: [Errno 2] No such file or directory: 'text.txt'.
Why? because I used relative path and there is no text.txt in the tmp directory.
Then what are the solutions?
Option-1: use open("inside_directory/text.txt") instead of open("text.txt").
Option-2: first go to inside_directory then use open("text.txt") and run your script with python pfile.py.
Option-3: Use absolute path. You can access that file regardless of where you are now.

FileNotFoundError: [Errno 2] No such file or directory: 'test_text.txt'

The file "test_text.txt" is in the same folder as the program. it's a seperate folder from everything else that just has the text file and the program. unless I am misunderstanding what the error means, I just can't figure out what i'm missing or did wrong. i'm just reading a basics python book and this I pretty much copied it 1 for 1.
CODE
with open('test_text.txt') as test_text:
reader = test_text.read()
print (reader)
FileNotFoundError means you are trying to open a file that does not exist in the specified directory (in this case, whatever directory you are running your Python script from).
the file "test_text.txt" is in the same folder as the program. it's a seperate folder from everything else that just has the text file and the program
In that case, you need to make sure that you're in the same directory as your file on your command-line, or specify the full path to test_text.txt (e.g. /home/user/Desktop/test_text.txt)
FileNotFoundError occurs when you are trying to access files outside the scope of the application. In this case, the scope of your application is the folder in which you main python file resides.
There are couple of ways through which you can resolve this:
1. Provide an absolute or full path to the file: As mentioned by #pigeonburger in his answer, you can provide a full path. If you are using windows, then full path will be from C:/Desktop/
2. Using a Relative Path: Relative paths is the solution if both the file you want to access and the python program are in the same folder. Relative Paths start with ./ So, based on your file structure, the possible solution is this:
with open('./test_text.txt'):
# your code
Thank you Pointman for your answer. It did not work for me but it did lead me to what worked. I am using Visual Studio Code and I was having this issue with opening a text from a Udemy course.
jabber = open('Jabberwocky.txt', 'r')
for line in jabber:
print(line)
jabber.close()
After running this code I would get the following error message
FileNotFoundError: [Errno 2] No such file or directory: './Jabberwocky.txt'
I tried both the absolute path and the relative path option but they did not work for me. What I found that worked was once the terminal is open using Python you will have to traverse through your files until you get to the said folder or file. Once you do this then run the code and it will work.

Load .mat file in python

I am working with on a course by coursera, and wanted to try the scripts locally.
I got this error:
IOError: [Errno 2] No such file or directory: 'datasets/data.mat'
The file is in the directory and I can see it from Finder on MAC, however, it's not visible form the file explorer in Spyder.
I found this question: Read .mat files in Python
and checked that original module according to what is suggested in the first answer, but still the file cannot be reached.
I don't know if it's worth mentioning, but the file has a shortcut icon (even though I've downloaded it directly from coursera).
Why the file is not reachable? and how reach it?

Python why is file path considered a directory

I am working on a Django project where I want to replace a file on my server. One way of doing it is to remove the file if the filename already exists and saving the new file at its place.
My problem is that my file path that ends with /data.xlsx is considered a directory. In python, os.isfile(path) returns false and os.isdir(path) returns true, though the path is this:
path = os.path.normpath(os.path.join(settings.MEDIA_ROOT, name)).replace('\\', '/')
which returns something like
"G:/username/path/to/django/project/static/data/data.xlsx"
os.remove(path) returns an OSError because path is not a file path supposedly. And I checked and I have all permissions on this file (0777).
I don't know how to make python understand that this is an Excel file and not a directory. Can someone help me? I don't have any debugging ideas left.
Thanks in advance!
A path name does not define whether something is a file or directory. You could name a file "/tmp/my/directory" and a directory "/tmp/my/file.xls" if you felt like it. Likewise, python doesn't decide whether a path corresponds to a file or a directory, it only checks. If python says the file corresponds to a directory, that means your OS says it's a directory. Either your OS is seriously messed up, or you actually have a directory named path.
Hint: It's likely the later. Try removing the directory at path.

Categories

Resources