![](/img/trans.png)
[英]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.