I'm trying to import a.csv file with pandas. It works on jupyter notebook (.ipynb) but doesnt work on python files (.py). I use the following code in my python file to import the csv file:
import pandas as pd
df = pd.read_csv("business.csv")
print(df)
Yet, I receive the following error:
PS C:\Users\User\Desktop\Coding Folder> & 'C:\Users\User\AppData\Local\Programs\Python\Python39\python.exe' 'c:\Users\User\.vscode\extensions\ms-python.python-2021.2.582707922\pythonFiles\lib\python\debugpy\launcher' '51427' '--' 'c:\Users\User\Desktop\Coding Folder\Python Projects\Pandas\csvReading.py'
Traceback (most recent call last):
File "c:\Users\User\Desktop\Coding Folder\Python Projects\Pandas\csvReading.py", line 2, in <module>
df = pd.read_csv("business.csv")
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 605, in read_csv
return _read(filepath_or_buffer, kwds)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 457, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 814, in __init__
self._engine = self._make_engine(self.engine)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 1045, in _make_engine
return mapping[engine](self.f, **self.options) # type: ignore[call-arg]
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 1862, in __init__
self._open_handles(src, kwds)
File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\pandas\io\parsers.py", line 1357, in _open_handles
self.handles = get_handle(
File "C:\Users\User\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: 'business.csv'
Can anyone help me out, I would really appreciate it. Thank you
Based on the information you provided, I reproduced the problem you described. As people mentioned, the reason lies in the file path. The VS Code terminal is at the project folder level, therefore, it cannot find files in the inner folder in this folder.
Please refer to the solution as follows:
Since you are using debugging, you could use " "cwd": "${fileDirname}",
" in " launch.json ":
If you want to click the run button on the top right of VS Code, we need to use "python.terminal.executeInFileDir": true,
in " settings.json ":
They all make the terminal enter the folder where the currently executed file is located, so please make sure that the "csv" file and the "python" file are in the same folder.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.