CSV file upload causing FileNotFoundError on macbook - python

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

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

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.

VScode isn't creating a file with python

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

Cant read csv files using pandas in Visual Code

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.

Debugger Differences: VSCode / Terminal (Python)

I simply wanted to read a JSON file using this code:
import json
with open("file.json") as File:
print(json.load(File.read()))
When I try to run it using the VSCode debugger, I get the error:
[Errno 2] No such file or directory: 'file.json'
But when I run it from the Terminal using python file.py it works.
The problem is, that VSCode somehow uses an other "Working Directory" because when I run os.getcwd() in VSCode, I get the path to the parent folder of the folder the python script is in.
When I run it from the Terminal, I get the right path.
Why is that?
Just to point out: the problem is NOT the print statement/json. The same error shows up when I only try to open the file without anything else and then pass.
import os
dir_path = os.getcwd()
Use this to get your current directory. You'll be able to discern where Python is running from. You can also use the full filepath for your JSON file.
import json
with open("fullpath/to/json/file/file.json") as File:
print(json.loads(File.read()))
As discussed in comments, your problem turned out to be the environment you were working in. When the program was executed from terminal, it worked and found the file. That is most likely because of the way your virtual environment is set up in VS Code. A virtual environment or venv as it is called, is an isolated environment of Python interpreter separate from your global Python install. It is useful when you are working on two different projects that require different versions of libraries. For example; a project that uses Django 1.10 and one that uses 1.9, so you don't have to keep shuffling between installing and uninstalling them.
A virtual environment is a directory tree which contains Python
executable files and other files which indicate that it is a virtual
environment.
As explained here, your .json file was most likely outside of your project virtual environment, and that is why it could not find it. I hope that helps you understand it.
You can verify if a file exists using os package:
import os.path
print(os.path.isfile("file.json"))
this should print True if the file exists.
Also you can try using the absolute path to make sure.
import json
with open('/Users/my_pc/Downloads/example_2.json') as f:
data = json.load(f)
print(data)
you can load your json by this way and give exact path of your directory
where your json file is present.

Categories

Resources