python-3.x - 无法在 Python 上打开和导入文件
问题描述
我这个月才开始学习 Python,之前没有任何编码知识。在我开始练习编码之前,我就被打开和导入文件所困扰!为此,我不知道如何解决作为新手的问题,所以我会尽力解释。
1. 打开文件。
工作目录正确。csv 文件是在 Ubuntu 操作系统上使用 Geany 创建的。
name=[]
age=[]
height=[]
with open(’/home/stephanie/chapter5exercise.csv’, encoding=’utf-8’,mode=’r’,newline=’’) as
csv file:
reader = csv.reader(csvfile, delimiter=’,’)
for row in reader:
name.append(row[0])
age.append(row[1])
height.append(row[2])
print("Done!")
**Error message:**
File "<ipython-input-3-f801fef0d65e>", line 5
with open(’/home/stephanie/chapter5exercise.csv’, encoding=’utf-8’,mode=’r’,newline=’’) as
^
SyntaxError: invalid character in identifier
2. 导入文件
我下载了一个 whatsapp 聊天文本文件以进行编码练习。它存储在我的桌面环境中。但是,当我尝试在 Jupyter Notbook 上导入它时,它不起作用。
我相信这是问题所在,尽管我不知道如何解决这个问题:
txt 文件的位置,如我的终端所示: /Users/Mac/Desktop
而 Jupyter 上显示的工作目录是: /home/stephanie/Desktop
with open ("_chat.txt") as f:
data = f.readlines()
**error message:**
---------------------------------------------------------------------------
FileNotFoundError Traceback (most recent call last)
<ipython-input-5-07cb5c381c47> in <module>
----> 1 with open ("_chat.txt") as f:
2 data = f.readlines()
FileNotFoundError: [Errno 2] No such file or directory: '_chat.txt'```
Any input or advice is greatly appreciated. Thanks very much in advance!