0
count = 0
answer = ""
pass_pool={"CSRP":"","pos":"","erp":"","comverse":"","miki":"","citrix":""}
name = ""


def program_start():
    answer = input('Do you want to make some TXT with the same passwords? y\\n :')
    count = int(input('How many TXT files do you want to make?'))
    name = input('Enter the hot user id:')
    name = name+".TXT"
    password_collector() # collect password to pass_pool dictionary
    create_file() #create TXT file. it has to be in capital "TXT" 
    #for the safe program.

def create_file():
    newTXT = open(name, "w")
    newTXT.write(name + "\n \n" )
    for system , password in pass_pool.items():
        newTXT.write(system + ":" + password )
    newTXT.close()

我得到:

File "C:\Python33\mypy\txt creator.py", line 16, in create_file
newTXT = open(name, "w")
FileNotFoundError: [Errno 2] No such file or directory:

从我在谷歌上看到的这个错误意味着找不到错误的路径或文件。但是我检查并在我的路径中sys.path看到了这一点,并且我使用它创建了文件,因此它应该可以正常工作。"C:\Python33\mypy""w"

当我只使用create_file()shell 中的函数时,它可以正常工作。

4

1 回答 1

2

当您设置 in 的值时nameprogram_startPython 会name在该函数的范围内创建一个局部变量,该变量会屏蔽 global name,因此全局值保持不变。在create_file您使用未更改的 globalname时,它等于"",并且打开具有该名称的文件""会给您一个错误。

快速而肮脏的修复将添加

global name

在开始program_start。但是写的更清楚

count = 0
answer = ""
pass_pool={"CSRP":"","pos":"","erp":"","comverse":"","miki":"","citrix":""}

def program_start():
    answer = input('Do you want to make some TXT with the same passwords? y\\n :')
    count = int(input('How many TXT files do you want to make?'))
    name = input('Enter the hot user id:')
    name = name+".TXT"
    password_colector() # collect password to pass_pool dic
    create_file(name) #create TXT file. it has to be in capital "TXT"
    #for the safe pogram.

def create_file(name):
    newTXT = open(name, "w")
    newTXT.write(name + "\n \n" )
    for system , password in pass_pool.items():
        newTXT.write(system + ":" + password )
    newTXT.close()
于 2013-09-09T06:16:55.687 回答