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

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.

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

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.

New path is not reflected in Python code

I have a code in which I am loading a text file from a folder. The code looks like this:
snap1 = np.loadtxt("../data/milli_17")
snap2 = np.loadtxt("../data/milli_19")
So the milli17 and milli19 files are located in a folder that is located in the same folder with my working folder. So far everything was good. However, I moved the data folder inside the working directory so the directory placement became like this: /Workingdirectory/data/
So I went ahead and reflected that on the code by removing the two dots so it wouldn't go up one directory:
snap1 = np.loadtxt("/data/milli_17")
snap2 = np.loadtxt("/data/milli_19")
However now when I run the code I get an error saying the directory does not exist:
IOError: [Errno 2] No such file or directory: '../data/milli_17'
but debugging shows the line of error as this:
----> 4 snap1 = np.loadtxt("/data/milli_17")
Couldn't get my head around it, all seemed OK to me. Where do I make the mistake?
Edit:
I don't think the problem has something to do with how I write down the path. The problem is that it doesn't matter what I put there, the code still (as seen in the error code) goes and checks the old directory.
If you restart the kernel that should resolve your issue.
I believe you have to use the file extension and no forward slash before the data folder.
snap1 = np.loadtxt("data/milli_17.txt")

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