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
Related
I am trying to upload a CSV file using python on a MacBook. I've tried multiple platforms such as jupyter notebook, google colab and pycharm but none have worked with uploading my csv file.
I tried to use the regular method of importing:
df = pd.read_csv(r'/Users/trevorwilliams/Desktop/germany.csv')
print (df)
I got:
FileNotFoundError: [Errno 2] No such file or directory: '/Users/trevorwilliams/Desktop/germany.csv'
Is there anything I need to change or do with the pathname? I just used the right click and alt/option when copying the pathname before pasting it.
I've also tried to change the characters from backslash to forward slash and inserting two slashes and tried including /.rb/ after .csv to see if that would work but still didn't
and I also tried checking if the OS path existed and it said it was false.
Any suggestions or tips on what I could be doing wrong?
Run this command to check in which directory you are currently are:
import os
os.getcwd()
If not in the same directory,
run this command
os.chdir(‘/Users/trevorwilliams/Desktop/’)
You can then import the CSV using Pandas
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.
I'm using this code to create a file with "AAA" written in it. It's working in the regular python IDLE but isn't working in VScode.
with open("another.txt","w") as myfile:
myfile.write("AAA")
If no error is reported, then the file is successfully created somewhere. As you are using a relative path the file is probably saved to current working directory.
You can find it either via the path displayed in the terminal you are using, or in your Python code:
from pathlib import Path
print(Path.cwd())
Python : 3.5
IDE : Visual code
Platform : win 10 64 bit
First i created a virtual env _kerasVenv and then activated the env and then installed pandas using pip.
This is my directory structure:
I added a python script in Exercise files folder where I am trying to read .csv file using pandas
test= pd.read_csv('test.csv', encoding='utf-8')
.csv file and python script are in the same folder so wrong path is not the issue.But i am getting below error:
Unable to open 'parsers.pyx': Unable to read file (Error: File not found (c:\users\anubhav.jhalani\downloads\ex_files_building_deep_learning_apps\pandas\_libs\parsers.pyx)).
Can someone explain why python is looking for pandas in c:\users\anubhav.jhalani\downloads\ex_files_building_deep_learning_apps folder and why parsers.pyx file does not exist in original pandas folder which are in _kerasVenv folder?
How can i get rid of this error?
Update: I found out while hovering on import pandas as pd statement that it is looking for pandas module in c:\users\anubhav.jhalani\downloads\ex_files_building_deep_learning_apps . Why it is happening?
I think it is easier to always give the total file path.
Instead of:
test= pd.read_csv('test.csv', encoding='utf-8')
try to use:
test = pd.read_csv('C:/users/anubhav.jhalani/downloads/ex_files_building_deep_learning_apps/test.csv', endcoding='utf-8')
this should help, you can also have a look here, what syntax you need to write it:
Windows path in Python
You can also get the full path in your windows explorer, if you are unsure where it is saved.
In my case it also wasn't able to find the file even in the same directory and with the complete path. I figured it out that this issue was happening only in my VSCode editor and when I opened the same notebook in jupyter lab it was perfectly working. So then I tried one thing that make it work for me in VSCode as well was that I put my file in the directory other than "C". For example I put my file in the "D" directory and in VSCode it becomes able to work fine.
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.