[英]python read all files from a folder and write the file name and other info into a txt file
[英]How to create python executable from a .py file that need to read and write .txt files in a separate folder
我创建了一个 python gui,可以将有关用户写入 .txt 文件的信息存储在名为加密密码的单独文件夹中。 所以它有像
file = open(file='encrypted passwords\\first password.txt', mode='w')
file.write('123456')
file.close()
和
password = open(file=('encrypted passwords\\first password.txt'), mode='r').read()
和
os.unlink('encrypted passwords\\first password.txt')
python 脚本和“加密密码”文件夹在同一个文件夹中,当我运行它时没有问题。
但是当我使用 pyinstaller 创建 .exe 文件时它不起作用,因为它没有“加密密码”文件夹!
如果我手动添加了一个“加密密码”文件夹,它会起作用,但是有没有办法不手动添加文件夹?
您可以检查文件夹是否存在并仅在它不存在时创建它
import os
if not os.path.exists('encrypted passwords'):
os.makedirs('encrypted passwords')
在较新的 Python 中,您可以使用exist_ok=True
在文件夹存在时跳过
import os
os.makedirs('encrypted passwords', exist_ok=True)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.