简体   繁体   English

Python找不到我的文本文件?

[英]Python can't find my text file?

I'm totally a noob at computer languages. 我完全不懂计算机语言。

In my school I downloaded a file called color.txt Then I was suppose to do some programming involving the file...... 在我的学校,我下载了一个名为color.txt的文件。然后我想做一些涉及该文件的编程工作……

import os.path
filename=input("Enter name of input file >")
infile=open(filename,"r")

and so on. 等等。 It worked at school. 它在学校工作。

Now I came home and downloaded the file again but this time the same python program that worked in school does not recognizes color.txt 现在我回到家并再次下载了文件,但是这次在学校工作的相同python程序无法识别color.txt

My teacher told the class to try 我的老师告诉全班尝试

import os.path
if os.path.isfile("color.txt"):
 print("color.txt") 

to see if it exist but apparently it doesn't. 看看它是否存在,但显然不存在。

To clarify both times I downloaded the file to the desktop. 为了澄清这两次,我将文件下载到了桌面。 At my school we use linux. 在我学校,我们使用linux。 At home I have windows. 家里我有窗户。 At home when you open the file it opens with notepad. 在家里,当您打开文件时,它将使用记事本打开。 In school it open with something else..I forgot. 在学校里它开着别的东西..我忘了。 Please help. 请帮忙。

For your program to find the file color.txt , your file needs to be in the same directory the program is executed from. 为了使程序能够找到文件color.txt ,您的文件必须与执行程序所在的目录相同。 If you downloaded the file to your desktop, and your program is executed from another directory, it won't find the file. 如果将文件下载到桌面,并且从另一个目录执行了程序,则找不到该文件。

Alternatively, when you enter the name of the file, enter the entire absolute path. 或者,当您输入文件名时,输入整个绝对路径。 For a file on your desktop, that should be something like C:\\Users\\username\\Desktop\\color.txt 对于您桌面上的文件,该文件应类似于C:\\Users\\username\\Desktop\\color.txt

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

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