' NameError' test'没有定义的。我哪里错了?

时间:2015-09-24 06:48:50

标签: python

我正在使用Python 2.7并试图让我的程序检查文件是否存在,如果存在,程序应该询问用户是否要覆盖它。如果文件不存在,则应创建新文件。在发现文件存在的情况下重复这两个步骤。这是代码:

import os.path
file_name = input("Please enter the name of the file to save your data       to: Example: test.txt ")
file_open = open(file_name, "w")
if os.path.isfile(file_name):
    print ("File exists")
    decide = input("Do you want to overwrite the file?, Yes or No")
    control = True
    while control:
        if decide != "Yes":
            file_name = input("Please enter the name of the file to save your data to: Example: test.txt ")
            if os.path.isfile(file_name):
                print ("File exists")
        else:
            newFile = open(file_name, "w")
            newFile.write(str(model))
            newFile.close()
            control=False
else:
    print("Creating a new file..................")
    file_open.write(str(model))
    file_open.close()

1 个答案:

答案 0 :(得分:0)

在第2行,第6行和第10行中,当你正在读取字符串时,它应该是raw_input(),并检查代码的缩进。