Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/email/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x Can';t在Python上打开和导入文件_Python 3.x - Fatal编程技术网

Python 3.x Can';t在Python上打开和导入文件

Python 3.x Can';t在Python上打开和导入文件,python-3.x,Python 3.x,我这个月刚开始学习Python,之前没有任何编码知识。在我开始练习编码之前,我就一直在打开和导入文件!为此,作为一个新手,我不知道如何解决这些问题,所以我会尽可能地解释 一,。打开文件 工作目录是正确的。csv文件是在Ubuntu操作系统上用Geany创建的 name=[] age=[] height=[] with open(’/home/stephanie/chapter5exercise.csv’, encoding=’utf-8’,mode=’r’,newline=’’) as csv

我这个月刚开始学习Python,之前没有任何编码知识。在我开始练习编码之前,我就一直在打开和导入文件!为此,作为一个新手,我不知道如何解决这些问题,所以我会尽可能地解释

一,。打开文件

工作目录是正确的。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

name=[]
年龄=[]
高度=[]
打开('/home/stephanie/chapter5exercise.csv',encoding='utf-8',mode='r',newline=''')作为
csv文件:
reader=csv.reader(csvfile,分隔符=',')
对于读取器中的行:
name.append(第[0]行)
age.append(第[1]行)
追加高度(第[2]行)
打印(“完成!”)
**错误消息:**
文件“”,第5行
打开('/home/stephanie/chapter5exercise.csv',encoding='utf-8',mode='r',newline=''')作为
^
SyntaxError:标识符中的字符无效
二,。导入文件

我下载了一个whatsapp聊天文本文件进行编码练习。它存储在我的桌面环境中。然而,当我试图在Jupyter Notbook上导入它时,它没有工作

我相信这就是问题所在,尽管我不知道如何解决这个问题:

txt文件的位置如我的终端所示: /Users/Mac/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! 

而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! 

打开(“_chat.txt”)作为f:
数据=f.读线()
**错误消息:**
---------------------------------------------------------------------------
FileNotFoundError回溯(最近一次调用上次)
在里面
---->1打开(“_chat.txt”)作为f:
2数据=f.读线()
FileNotFoundError:[Errno 2]没有这样的文件或目录:'\u chat.txt'```
非常感谢您的任何意见或建议。非常感谢!

对于第一个错误,请尝试更改开放行中的撇号。例如,将
'/home/stephanie/chapter5exercise.csv'
更改为
'/home/stephanie/chapter5exercise.csv'
。 显然,您使用的撇号(')不是正确的


对于第二个错误,请尝试给出txt文件的整个路径

第一个原因是,
是一个奇怪的引语,Python不知道。使用标准
s。第二,仔细检查你的路径,如果你想确定的话,使用绝对路径。谢谢你,巴拉特。在你的帮助下,我能够轻松地修复第一个错误。但是,对于第二个错误,尽管我尝试使用相对路径和绝对路径,它仍然不起作用。第二个错误是否与我在终端上看到的“/Users/Mac/Desktop”而不是“/home/stephanie/Desktop”有关?我很难将文件路由到正确的路径,因为我不能简单地执行类似“Desktop/_chat.txt”的操作。你对此有什么建议吗?谢谢。好的,一个最简单的方法是将它上传到你的Jupiter笔记本,点击文件>>打开,然后上传_chat.txt,这样你就可以在你的代码块中直接给出文件的本地路径(例如File='/home/jovyan/FORMULA.rtf')。如果不确定路径,可以使用选项卡。