pandas cannot read csv in same directory - python

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']

Related

Python: "../DATA/file.csv" no longer working for file opening

Since a while, I have been using the short syntax ('../DAT/file.csv') to get to files under DATA folder. Since this morning, it is not working anymore and I am getting the following error:
FileNotFoundError: [Errno 2] No such file or directory: '../DATA/file.csv'
Any thoughts? Code I am using is below:
Thanks in advance,
df = pd.read_csv('../DATA/moviereviews.csv')
Check if your current directory is the one you expect it to be (i.e. the one that is beside the DATA directory). You can use the following to do so:
import os
print(os.getcwd())
Check that DATA/moviereviews.csv actually exists.

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.

Download file with urlretrieve() to subfolder

Is it possible to use urlretrieve() in order to download something into a subfolder without expressing it in an absolute but relative manner?
For example:
urllib.request.urlretrieve(url, '/downloads/2017/foo.txt')
Everytime I add a path to the filename python throws following error:
File "C:\Program Files\Python36\lib\urllib\request.py", line 258, in urlretrieve
tfp = open(filename, 'wb')
FileNotFoundError: [Errno 2] No such file or directory: '/downloads/2017/foo.txt'
But when I use this code:
urllib.request.urlretrieve(url, 'foo.txt')
it happily downloads the file.
I think I am missing something basic here, but after searching the interwebs for quite a while I haven't found an answer yet. Does anyone know how relative filepaths should be expressed in the urlretrieve() method?
When using urllib.request.urlretrieve(), you need to make sure that, if you want to save the file somewhere else than the program's location, the folders you are referencing already exist. Alternatively you can use the following code to generate the folders you need if they do not exist:
for i in directories:
if not os.path.isdir(i):
os.makedirs(i)
Check for spelling mistakes if you still get a FileNotFoundError.

filenotfound error python (running through atom)

I'm working my way through the python crash course pdf. Everything was going well until I hit chapter 10 "files and exceptions".
The task is very simple.
1) create a text file "pi_digits.txt" that contains the first 30 digits of pi.
2) run the following code:
with open('pi_digits.txt') as file_object:
contents = file_object.read()
print(contents)
I keep getting a filenotfounderror [errno 2].
I have tried using the full file path, placing the file in the same ~.atom folder that contains the package 'script'.
I tried to run the file through a terminal and got the same error message.
I also searched stackoverflow for solutions and did find similar problems but the answers did not work.
Any help would be appreciated.
Prepend this:
import os
print(os.getcwd())
os.chdir('/tmp')
and copy the .txt file to /tmp. Also, be sure the copied filename is all lowercase, to match your program.

import csv into pandas dataframe

I am facing difficulty importing a csv file to python from my desktop. It seems that the file or the location is not being recognized while reading.
Have tried several different methods to import, but every time it gives the same error:
IOError: [Errno 2] No such file or directory: '/Users/uditasingh/Desktop/Analysis/monthly_visits.csv'
for the code:
import csv
cr = csv.reader(open("/Users/uditasingh/Desktop/Analysis/monthly_visits.csv","rb"))
I have obtained the path of the csv file from the file 'properties'.
Don't understand what seems to be going wrong.
Please help!
Thanks
My bet is that it searches for the Users directory in the code's working dir and obviously can't find it. Try to use the full path, ie 'C:/Users/......`.
try to write
cr = csv.reader(open("\Users\uditasingh\Desktop\Analysis....))

Categories