Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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 File txt:FileNotFoundError:[Errno 2]没有这样的文件或目录_Python - Fatal编程技术网

Python File txt:FileNotFoundError:[Errno 2]没有这样的文件或目录

Python File txt:FileNotFoundError:[Errno 2]没有这样的文件或目录,python,Python,我有一个txt文件,如果我试图打开它,python会说: runfile('/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python/untitled0.py', wdir='/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python') Traceback (most recent call last): File "<ipython-input-30-b4b

我有一个txt文件,如果我试图打开它,python会说:

runfile('/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python/untitled0.py', wdir='/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python')

Traceback (most recent call last):

  File "<ipython-input-30-b4bdfdd17ca2>", line 1, in <module>
    runfile('/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python/untitled0.py', wdir='/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python')

  File "/Users/costanzanaldi/anaconda/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 685, in runfile
    execfile(filename, namespace)

  File "/Users/costanzanaldi/anaconda/lib/python3.4/site-packages/spyderlib/widgets/externalshell/sitecustomize.py", line 85, in execfile
    exec(compile(open(filename, 'rb').read(), filename, 'exec'), namespace)

  File "/Users/costanzanaldi/Desktop/tesi/Tesi_Naldi/COdice _Python/untitled0.py", line 13, in <module>
    in_file = open("POLO_SCIENTIFICO_(LAMMA).txt","r")

FileNotFoundError: [Errno 2] No such file or directory: 'POLO_SCIENTIFICO_(LAMMA).txt'
runfile('/Users/costanzanaldi/Desktop/tesi/tesi\u Naldi/COdice\u Python/untitled0.py',wdir='/Users/costanzanaldi/Desktop/tesi/tesi\u Naldi/COdice\u Python')
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
运行文件('/Users/costanzanaldi/Desktop/tesi/tesi\u Naldi/COdice\u Python/untitled0.py',wdir='/Users/costanzanaldi/Desktop/tesi/tesi\u Naldi/COdice\Python')
文件“/Users/costanzanaldi/anaconda/lib/python3.4/site packages/spyderlib/widgets/externalshell/sitecustomize.py”,第685行,在runfile中
execfile(文件名、命名空间)
文件“/Users/costanzanaldi/anaconda/lib/python3.4/site packages/spyderlib/widgets/externalshell/sitecustomize.py”,执行文件第85行
exec(编译(打开(文件名'rb').read(),文件名'exec'),命名空间)
文件“/Users/costanzanaldi/Desktop/tesi/tesi\u Naldi/COdice\u Python/untitled0.py”,第13行,在
in_file=open(“POLO_SCIENTIFICO(LAMMA).txt”,“r”)
FileNotFoundError:[Errno 2]没有这样的文件或目录:“POLO_SCIENTIFICO(LAMMA).txt”
1) 文件存在!
2) 这条路是正确的!它在桌面上

您需要将目录更改为桌面才能访问该文件。您可以使用
os
模块执行此操作,如下所示:

import os
os.chdir("/path/to/Desktop")

这可能意味着该文件不在桌面上。(我假设您已将Ethan的“/path/to/Desktop”更改为系统中的正确路径,即“/Users/costanzanaldi/Desktop”)。无论它在哪里,您都需要为open()提供POLO_SCIENTIFICO(LAMMA).txt的完整路径,除非您已将chdir指定给正确的目录。我想您在POLO_SCIENTIFICO(LAMMA.txt:-)中的字母大小写是正确的