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

时间:2015-11-04 17:16:17

标签: 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-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'

1)文件EXISTS! 2)路径是正确的!它在桌面上!

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

据推测,这意味着该文件不在桌面上。 (我假设您将Ethan&#34; / path / to / Desktop&#34;更改为系统中正确的路径,即&#34; / Users / costanzanaldi / Desktop&#34;?)。无论在哪里,你都需要给open()提供POLO_SCIENTIFICO_(LAMMA).txt的完整路径,除非你有chdir到正确的目录。我假设你在POLO_SCIENTIFICO_(LAMMA).txt中得到了正确的字母大小写: - )