Python 要输出到文件的密码生成器

Python 要输出到文件的密码生成器,python,python-3.x,python-2.7,Python,Python 3.x,Python 2.7,我有一个学校的大项目,在那里我会得到一个整数的输入,有了这个整数,我会使用随机字符来输出所要求的长度。我有一个问题,我将创建一个名为“output.txt”的文件,其中包含一个“a”,用于将结果附加到名为output.txt的文件中。我想要的是,在结果存储到文件中之后,我想要显示它 print(password_generator(user_input_converted), file=file) 打两次电话给print。一次使用文件,一次不使用 password = password

我有一个学校的大项目,在那里我会得到一个整数的输入,有了这个整数,我会使用随机字符来输出所要求的长度。我有一个问题,我将创建一个名为“output.txt”的文件,其中包含一个“a”,用于将结果附加到名为output.txt的文件中。我想要的是,在结果存储到文件中之后,我想要显示它

    print(password_generator(user_input_converted), file=file)

打两次电话给print。一次使用
文件,一次不使用

password = password_generator(user_input_converted) 
print(password, file=file)
print(password)
假设
password\u生成器
正在返回密码-您的代码似乎暗示了这一点。

使用open()如下:


password\u生成器
函数返回您想要显示的内容,然后它将按您所说的那样工作。成功了!由于某种原因,当我尝试时,它给了我文件类型和文件的其他参数,如utf=8。但是谢谢!
passFile = open(file, 'a')
password = password_generator(user_input_converted)
passFile.write(password + '\n')
passFile.close()
print('The password is:', password)