简体   繁体   English

未定义'NameError'test'。 我要去哪里错了?

[英]'NameError 'test' not defined. where am I going wrong?

I am using Python 2.7 and am trying to get my program to check if a file exists and if it does, the program should then ask the user if they want to overwrite it. 我正在使用Python 2.7,并试图让我的程序检查文件是否存在,如果存在,则程序应询问用户是否要覆盖它。 If the file is not there, a new one should be created. 如果文件不存在,则应创建一个新文件。 These two steps are repeated where the file is found to be existing. 如果发现文件存在,则重复这两个步骤。 Here is the code: 这是代码:

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()

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

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

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