简体   繁体   中英

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......

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

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. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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