Having issues reading a .csv file present in a directory - python

I have a following path defined where I have
label_csv_file = r"C:/Users/username/Downloads/Personal Stuff/Python Directory files/Scripts/CSV Copy Scripts/label.csv"
I was trying to read the csv using pandas,
import pandas as pd
labelpd = pd.read_csv(label_csv_file)
I get the following error:
handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/username/Downloads/Personal Stuff/Python Directory files/Scripts/CSV Copy Scripts/label.csv', but the file is clearly there
I blacked out my name, instead of username my name is in the path, so I don't understand why I am having the issue, I have changed the label.csv to be just label as well. Its not just pandas, I was having issues opening with even with
open(label_csv_file, 'rb'),
where the label_csv_file path had been set, what is the issue here?
I have tried changing the file name to just label as it is already a .csv file that does not work either, I have changed the file path to just say /label, that doesn't work either

Related

FileNotFoundError: [Errno 2] File b'filepath.csv' does not exist:

I created a df to read a csv in my jupyter notebook library but for some reason the i'm getting an error like this:
FileNotFoundError: [Errno 2] File b'file path/filename.csv' does not exist: b'/file path/filename.csv'
My code is this:
df_csv = pd.read_csv('../file_path/file_name.csv', low_memory=False)
I believe I am using the absolute path to this file...I also checked the directory to confirm I was using the correct path.
import os
print(os.path.abspath('file_name.csv'))
and the output was:
/YXRwLW5vdGVib29rLW5pbmd1eWVuLXNlc3Npb24teGJ1YQ==/file_path/file_name.csv
Is that not the same as '../file_path/file_name.csv'? Would appreciate any troubleshooting tips...
I am not sure why this worked but instead of using the absolute path I used the relative path and just read the file name...A bit concerned with this method since I'd rather be explicit

pandas cannot read csv in same directory

i am having this issue since like 2 months, it didnt bother me at first but now that im trying to import a file with pd or even a normal txt file with open() it gives me this Exception:
File "C:\Users\lcc_zarkos\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\common.py", line 642, in get_handle
handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'marketing.xlsx'
if i use the full path it just runs normally.
people would say "just use full path then" but this is a really bad solution when it comes to using this program on multiple devices with different paths or stuff like that
so i hope you have any solutions.
here is the code:
import pandas as pd
df = pd.read_csv('marketing.xlsx')
image:
vscode
folder
edit:
it has none to do with the code itself but more like windows or my pc
FileNotFoundError means that the file path you've given to pandas point to an non existing file.
In your case this can be caused by:
you haven't put your file in the current working directory of your script
you have a typo in your file name
In both case, I would print the file path, and check the location using a file browser, you will find your mistake:
print(os.path.join(os.getcwd(), 'marketing.xlsx'))
i see a spaces in the file name there. I tried on mac, and it works very well.
['main.py', 'marketing .xlsx', 'requirements.txt']

Importing DataFrames - no such file or directory

I am trying to import a csv file as a dataframe into a Jupyter notebook.
rest_relation = pd.read_csv('store_id_relation.csv', delimiter=',')
But I get this error
FileNotFoundError: [Errno 2] No such file or directory: 'store_id_relation.csv'
The store_id_relation.csv is definitely in the data folder, and I have tried adding data\ to the file location but I get the same error. Whats going wrong here?
Using the filename only works if the file is located in the current working directory. You can check the current working directory using os.getcwd().
import os
current_directory = os.getcwd()
print(current_directory)
One way you can make your code work is to change the working directory to the one where the file is located.
os.chdir("Path to wherever your file is located")
Or you can substitute the filename with the full path to the file. The full path would look something like C:\Users\Documents\store_id_relation.csv.
Always try to pass the full path whenever possible
By default, read_csv looks for the file in the current working directory
Provide the full path to the read_csv function
However in your case , data and Data are different , and file paths are case sensitive
You can try the below path to fetch the file and convert it into a dataframe
rest_relation = pd.read_csv('Data\\store_id_relation.csv', delimiter=',')
In case where you don't want to change your read_csv()'s parameter, just make sure you are in a correct path of directory which your .csv file is in. Otherwise you can change your directory into there and then run the python file or simply change read_csv()'s parameter.

How do I get Pandas to recognize a *.zip file?

Objective: I want to read a CSV file contained in a *.zip file
My problem is that Python acts as there is not file.
FileNotFoundError: [Errno 2] No such file or directory: 'N:\\l\\xyz.zip'
The code I am using is the below (using a windows machine):
import pandas as pd
data = pd.read_csv(r"N:\l\xyz.zip",
compression="zip")
Any help would be appreciated?
data = pd.read_csv("N:\l\xyz.zip",
compression="zip")
EDIT:This is on an s3 bucket.
I have used below code for the zipped file in working directory & it worked
df = pd.read_csv("test_wbcd.zip", compression="zip")
So, the problem should be with the file path. Kindly check your file location.

Python file doesn't exist--verified correct working directory

Reading a csv with pandas, I get the error: 'IOError: File /test.csv does not exist.' Here is how I am reading:
var1 = pd.read_csv("test.csv")
I verified that I am in the correct working directory with:
print(os.getcwd())
Which prints C:\Users\name\folderName, and the test.csv file is in folderName. I also check if the file exists using os.path.isfile('test.csv') and that returns false. What am I doing incorrectly? I have tried using the full path as well, it did not work.
Edit: I tried opening a .txt file with the same format and everything was fine, so the problem seems to be with the fact that I'm opening a .csv.
The correct path would be
os.path.join(os.getcwd(), test.csv)
os.getcwd() gives you the path C:\Users\name\folderName and then we join the filename to make the complete path.
Problem was file name 'test.csv' and type csv rather than 'test' with type csv.

Categories