我很感激这个简单代码的帮助:
import csv
with open('CHTR_trades.csv','rb') as csvfile:
chtrreader = csv.reader(csvfile, delimiter = ' ', quotechar = '|')
for row in chtrreader:
print ', '.join(row)
我收到错误:
Traceback (most recent call last):
File "/home/peter/PycharmProjects/practice/reader.py", line 4, in <module>
with open('CHTR_trades.csv','rb') as csvfile:
IOError: [Errno 2] No such file or directory: 'CHTR_trades.cs
我从终端检查了文件的位置,它位于正确的文件夹中:
peter@peter-HP-G60-Notebook-PC:~/PycharmProjects/practice$ ls
CHTR_quotes.csv CHTR_stdev.csv graph.py plot.py practice.py
CHTR_quotes.short.csv CHTR_trades.csv matplotlibrc practice1.py reader.
非常感谢任何帮助。
答案 0 :(得分:0)
错误告诉您Python没有足够的信息来查找文件。您只需提供完整路径 1 :
即可解决问题with open(os.path.expanduser('~/PycharmProjects/practice/CHTR_trades.csv'),'rb') as csvfile:
或者,您可以使用os.chdir
并更改Python当前的工作目录:
from os import chdir
os.chdir(os.path.expanduser("~/PycharmProjects/practice/"))
with open('CHTR_trades.csv','rb') as csvfile:
...
无论哪种方式,您都需要向Python提供有关文件所在位置的更多信息。
1 在上面的两个代码示例中,os.path.expanduser
用于将文件路径中的~
扩展到您的主目录。