[英]How to create python executable from a .py file that need to read and write .txt files in a separate folder
I've created a python gui that can store information about the user writing .txt files in a separate folder named encrypted passwords.我创建了一个 python gui,可以将有关用户写入 .txt 文件的信息存储在名为加密密码的单独文件夹中。 So it has lines like
所以它有像
file = open(file='encrypted passwords\\first password.txt', mode='w')
file.write('123456')
file.close()
and和
password = open(file=('encrypted passwords\\first password.txt'), mode='r').read()
and和
os.unlink('encrypted passwords\\first password.txt')
The python script and the 'encrypted passwords' folder are in the same folder and when I run it get no problems. python 脚本和“加密密码”文件夹在同一个文件夹中,当我运行它时没有问题。
But when I have created the .exe file using pyinstaller it did not work because it had no 'encrypted passwords' folder!但是当我使用 pyinstaller 创建 .exe 文件时它不起作用,因为它没有“加密密码”文件夹!
If I added manually a 'encrypted passwords' folder it worked but is there a way in order to not manually add the folder?如果我手动添加了一个“加密密码”文件夹,它会起作用,但是有没有办法不手动添加文件夹?
You can check if folder exists and create it only if it not exists您可以检查文件夹是否存在并仅在它不存在时创建它
import os
if not os.path.exists('encrypted passwords'):
os.makedirs('encrypted passwords')
In newer Python you can use exist_ok=True
to skip when folder exists在较新的 Python 中,您可以使用
exist_ok=True
在文件夹存在时跳过
import os
os.makedirs('encrypted passwords', exist_ok=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.