简体   繁体   English

Python找不到文件

[英]Python can't find file

I am having great difficulty getting python 3.4 to recognize a path or text file on a windows 8 system.我很难让 python 3.4 识别 Windows 8 系统上的路径或文本文件。 I have tried a variety of different approaches but get the similar errors (which likely implies something simple regarding the syntax).我尝试了各种不同的方法,但得到了类似的错误(这可能意味着语法很简单)。

The file itself is located in the same folder as the script file trying to open it: C:\Users\User\Desktop\Python stuff\Data.txt该文件本身与试图打开它的脚本文件位于同一文件夹中:C:\Users\User\Desktop\Python stuff\Data.txt

for simplicity, the simplest means to access the file (at least that I know of) is f=open为简单起见,访问文件的最简单方法(至少我知道)是f=open

These lines were coded as:这些行被编码为:

f = open("Data.txt", "r")

and

f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r") 

but return the error:但返回错误:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\Python stuff\Testscript.py", line 3, in <module>
    f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/User/Desktop/Python stuff/Data.txt'

When using Windows you want to keep in mind that if you name the file data.txt the file will actually be data.txt.txt , however Windows will show it as data.txt because windows hides the file extensions by default.使用 Windows 时,您要记住,如果您将文件命名为data.txt该文件实际上将是data.txt.txt ,但 Windows 会将其显示为data.txt因为 Windows 默认隐藏文件扩展名。 This option can be changed by following the steps in the first comment.可以按照第一条评论中的步骤更改此选项。

If you then search data.txt there really is no such file.如果您然后搜索data.txt确实没有这样的文件。

  • for f = open("Data.txt", "r") for f = open("Data.txt", "r")

Make sure your .py and .txt files are in the same directory.确保您的 .py 和 .txt 文件位于同一目录中。

  • for f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r") for f = open("C:/Users/User/Desktop/Python stuff/Data.txt", "r")

I think the space in Python stuff is messing things up.我认为Python stuff的空间把Python stuff搞砸了。

Update : I just tried this out .. seems to be fine with the space actually.更新:我刚试过这个……实际上空间似乎没问题。

>>> [i for i in open('/Users/pk-msrb/projects/temp/temp es/temp.txt')]
['1\n', '2\n', '3\n', '\n']

This is an old question, but thought I'd throw my two cents in anyway.这是一个老问题,但我想无论如何我都会投入我的两分钱。

I had this same issue.我有同样的问题。 I was using VS Code for my IDE, and had the folder setting to a folder above where I had the file.我在我的 IDE 中使用了 VS Code,并将文件夹设置为我拥有文件的文件夹。 This obviously was the issue.这显然是问题所在。 Once I opened the folder in VScode where the code and the text file were both located I was able to open the file with no issues.一旦我在 VScode 中打开了代码和文本文件所在的文件夹,我就可以毫无问题地打开文件。

Please check your current directory.请检查您的当前目录。

For that just do this:为此,只需这样做:

import os
print (os.getcwd())

The problem might be, where exactly you are executing the file.问题可能是,您正在执行文件的确切位置。

I had the same problem, but noticed, that the terminal in my IDE (VS Code) is executing the file in a different location.我遇到了同样的问题,但注意到我的 IDE(VS Code)中的终端正在不同的位置执行文件。 By simply cd to the files location in the terminal, everything went fine.通过简单地cd到终端中的文件位置,一切顺利。 ;) ;)

I had the same problem but now it works for me.我有同样的问题,但现在它对我有用。 I used to open a big map with a lot of sub-maps and the file was in one of the sub-maps.我曾经打开一个包含很多子地图的大地图,并且文件在其中一个子地图中。 Now I open the submap where my program and file.txt is located and the code open("file.txt", "r") works现在我打开我的程序和 file.txt 所在的子图,代码open("file.txt", "r")工作

I know this is an old question, but here is another option.我知道这是一个老问题,但这是另一种选择。

set your parent_folder if you have multiple files in the same folder:如果您在同一文件夹中有多个文件,请设置您的 parent_folder:

parent folder = "C:/Users/User/Desktop/"

extend the folder if necessary:如有必要,扩展文件夹:

import os.path
folder = os.path.join( parent_folder, "python stuff" )

add the filename:添加文件名:

file = os.path.join( folder, "Data.txt" )

open the file:打开文件:

if os.path.exists( file ):
    with open( file, "r"  ) as infile:
        data = infile.read()

or:要么:

import os
import os.path

# get the desktop location ( since this is a special folder )
desktop = os.path.join(( os.environ["userprofile"] ), "desktop" )
file = os.path.join( desktop, "python stuff", "data.txt" )
if os.path.exists( file ):
    with open( file, "r" ) as infile:
        data = infile.read()

I had a similar issue when using vs code.我在使用 vs 代码时遇到了类似的问题。 If you use the built-in green triangle to run, it seems to be able to run the code but it doesn't know where to look for the file.如果使用内置的绿色三角形运行,它似乎可以运行代码,但它不知道在哪里查找文件。 open an integrated terminal to the correct folder and run from the terminal line.打开一个集成终端到正确的文件夹并从终端行运行。 Seems like something vs code could figure out...似乎有些东西与代码可以弄清楚......

I had the same problem with pyCharm.我在 pyCharm 上遇到了同样的问题。 I found out that you sometimes have to specify the directory by going to edit config and working directory我发现您有时必须通过编辑配置和工作目录来指定目录

Try creating a file using:尝试使用以下方法创建文件:

f = open("myfile.txt", "x")

Then search your computer for "myfile.txt" which is the file you just created.然后在您的计算机中搜索"myfile.txt" ,这是您刚刚创建的文件。 Wherever the location of the file is, that is where your code is reading from :)无论文件的位置在哪里,这就是您的代码从中读取的位置:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM