Question

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

I get:

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

From what I look on google this error mean wrong path or file not found. But I check with sys.path and saw that "C:\Python33\mypy" in my paths, and I create the file with "w" so it should work with no problems.

When I used only the create_file() function in the shell it works with no problem.

Was it helpful?

Solution

When you set the value of name in program_start, Python creates a variable name local to that function's scope, which masks the global name, so the global value remains unchanged. In create_file you use the unchanged global name, which equals to "", and opening a file with the name "" gives you an error.

The quick-and-dirty fix would be adding

global name

in the beginning of program_start. But it is much clearer to write

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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top