2017-10-12 88 views
0

我试图打开一个使用此代码的文件,但它又一次显示错误:NameError:name“practice”未定义 其中“practice”是我试图打开的文件。 “practice.txt”(即时试图打开的文件)存在于创建我的python程序的同一目录中。无法使用python打开文件

import os 
name = input("Enter the name of the file : ") 
if(os.path.exist(name)==True): 
    fopen = open(name,'w') 
    line = fopen.readline() 
    while(line != ""): 
     print(line) 
     line=fopen.readline() 
    fopen.close() 
else: 
    print("file does not exist!!!") 
+0

是Python 2? – mentalita

+0

根据你的错误信息,你应该使用'raw_input' – Wondercricket

+0

python 2:'input'应该是'raw_input' –

回答

0

这是因为您必须指定文件的路径以及文件名。

I.E.打开(“文件/图像/ Image1.png”,“模式”)

+0

如果文件位于同一目录中,则不需要该路径 – Wondercricket

+0

True,@Wondercricket,但您也需要文件扩展名。也许这就是他忘了的。 – EgMusic

+0

我没有忘记扩展名。我正在写文件名和扩展名“.txt”@EgMusic –