在Pycharm中使用“在控制台中执行行”时无法使用相对路径

时间:2019-04-17 16:19:43

标签: python pycharm

我正在将Pycharm用于python项目。我的项目结构是这样的:

+ project
  + src
    - Data.csv
    - main.py

这是main.py

import panda as pd
dataset = pd.read_csv("Data.csv")

当我使用“在控制台中执行行”并运行第二行时,出现此错误:

Traceback (most recent call last):
  File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\IPython\core\interactiveshell.py", line 3296, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-14-bd7168d85704>", line 1, in <module>
    dataset = pd.read_csv('Data.csv')
  File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 702, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 429, in _read
    parser = TextFileReader(filepath_or_buffer, **kwds)
  File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 895, in __init__
    self._make_engine(self.engine)
  File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 1122, in _make_engine
    self._engine = CParserWrapper(self.f, **self.options)
  File "C:\Users\livw2\AppData\Local\Programs\Python\Python37\lib\site-packages\pandas\io\parsers.py", line 1853, in __init__
    self._reader = parsers.TextReader(src, **kwds)
  File "pandas\_libs\parsers.pyx", line 387, in pandas._libs.parsers.TextReader.__cinit__
  File "pandas\_libs\parsers.pyx", line 705, in pandas._libs.parsers.TextReader._setup_parser_source
FileNotFoundError: [Errno 2] File b'Data.csv' does not exist: b'Data.csv'

当我使用“运行”运行整个代码时,这很好,所以我认为在控制台中执行会更改目录。但是我还没有弄清楚如何解决。

1 个答案:

答案 0 :(得分:0)

要修复此问题,只需关闭当前项目并直接打开另一个包含文件的项目。