I'm new to python and the pandas library, and I'm facing this issue: Python's pandas library is not finding the file I'm trying to open, even though is in the same directory as the script. Until yesterday, I was using pandas and using the same lines of code, and it was working perfectly, so I'm very confused. I can run the script fine from a CMD window, but not from Jupyter Lab nor from VSCode. This is my code:
from pandas import *
london = read_csv('London_2014.csv')
print(london.head())
And this is the list of errors I get:
Traceback (most recent call last):
File "c:\Users\Lautte\Desktop\PY\OPEN UNIVERSITY\Learn-to-code-for-data-analysis-master\2_Cleaning_up_our_act\test2.py", line
3, in <module>
london = read_csv('London_2014.csv')
File "C:\Users\Lautte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
return func(*args, **kwargs)
File "C:\Users\Lautte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 680, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\Users\Lautte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 575, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\Lautte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 933, in __init__
self._engine = self._make_engine(f, self.engine)
File "C:\Users\Lautte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\parsers\readers.py", line 1217, in _make_engine
self.handles = get_handle( # type: ignore[call-overload]
File "C:\Users\Lautte\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\common.py", line 789, in get_handle
handle = open(
FileNotFoundError: [Errno 2] No such file or directory: 'London_2014.csv'
Thank you in advance, and have a nice week!
CodePudding user response:
Couple of beginner tips. Don't do star imports, they're considered bad practice just do pd.WhatYouWant i.e. pd.read_csv. Second, it works in CMD because the working directory is the same as the script, but not from vscode because you probably didn't properly change the directory of the terminal to be the folder where you have your script and the csv file. You could use absolute path instead of the relative like: london = read_csv("""c:\Users\Lautte\Desktop\PY\OPEN UNIVERSITY\Learn-to-code-for-data-analysis-master\2_Cleaning_up_our_act\London_2014.csv""") which should work anywhere.
CodePudding user response:
You can see this documentation:- https://pandas.pydata.org/docs/reference/api/pandas.read_csv.html
or the could be that you have given the wrong name for the file, or that you do not have that file in your current working directory.
CodePudding user response:
Do you still have the London_2014.csv
in the directory?
You can also try:
import pandas as pd
london = pd.read_csv('./London_2014.csv')
print(london.head())
or pass the absolute file path to the file London_2014.csv
to pd.read_csv
CodePudding user response:
You can re-write your code like this
import pandas as pd
london = pd.read_csv('London_2014.csv')
print(london.head())
and don't use from pandas import *