import csv into pandas dataframe - python

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

Related

FileNotFoundError: [Errno 2] No such file or directory: 'sp500_stocks.csv'

I am trying to import a csv file to python, but this message keeps me getting forward.
The code I entered was
import errno
import os
raise FileNotFoundError(
errno.ENOENT, os.strerror(errno.ENOENT), 'sp500_stocks.csv')
I tried entering the path but with no luck. This was the syntax,
df = pd.read_csv(r'C:/Users/(taking out name for privacy)/Desktop/starter_files/sp500_stocks.csv')
stocks = pd.read_csv('sp500_stocks.csv')
type(stocks)
Is there something that I am missing? Thank you so much in advance.
I was trying to create my own weighted portfolio by batching API calls through all stocks and having few more indicators and formulas to compute number of shares to buy for each share.
The code I used to this is mentioned above.
Based on your code given,I have no idea why this error happens but you can try move the file to your current working directory, using the relavant path to import this file.
# First move your file, then use
df = pd.read_csv('sp500_stocks.csv')

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.

Need help to open a file in python

I don't know what's wrong here, all I'm trying to do is to open this file, but it says it can't find such a file or directory, however as I have highlighted on the side, the file is right there. I just want to open it. I have opened files before but never encountered this. I must have missed something, I checked online, and seems like my syntax is correct, but I don't know.
I get the same error when I try with "alphabetical_words" which is just a text file.
When open() function receives a relative path, it looks for that file relative to the current working directory. In other words: relative to the current directory from where the script is run. This can be any arbitrary location.
I guess what you want to do is look for alphabetical.csv file relative to the script location. To do that use the following formula:
from pathlib import Path
# Get directory of this script
THIS_DIR = Path(__file__).absolute().parent
# Get path of CSV file relative to the directory of this script
CSV_PATH = THIS_DIR.joinpath("alphabetical.csv")
# Open the CSV file using a with-block
with CSV_PATH.open(encoding="utf-8") as csvfile:
pass # Do stuff with opened file
You need to import csv. Then you can open the file as a csv file. For example,
with open ("alphabetical.csv", "r") as csvfile:
Then you can use the file.
Make sure the file is in your cwd.

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

I am trying to read a csv file using pandas but it fails to recognize it - what could be the issue? (Tkinter)

I have been trying to read this csv file using pandas and then transfer it to a dictionary using:
pandas.read_csv("/Users/vijayaswani/Downloads/England1\ postcodes.csv ", index_col=1).T.to_dict()
but each time I get the error No such file or directory
neither does using the name of the file work and nor does using its path even though the file is not deleted or anything.
What could be the issue?
Looks like you have an extra space in the file path.
Have you tried:
pandas.read_csv("/Users/vijayaswani/Downloads/England1\ postcodes.csv", index_col=1).T.to_dict()
Or
pandas.read_csv("/Users/vijayaswani/Downloads/England1 postcodes.csv", index_col=1).T.to_dict()

Categories