① python項目文件(多個子文件,.py文件等)如何用pyinstaller打包為可執行exe文件
文件構成
使用pyinstaller打包的時候,僅打包.py文件,其餘依賴項只需在打包完成後,拷入打包生成的根目錄即可。
多文件打包
命令格式如下,下命令為一條命令,為方便顯示做了分行處理:
pyinstaller [主文件] -p [其他文件1] -p [其他文件2]
--hidden-import [自建模塊1]
--hidden-import [自建模塊2]
# 以上為一整條命令
以上文圖中結構為例,在根目錄打開命令窗口,輸入命令:
pyinstaller main.py -p mysql.py -p other.py --hidden-import mysql --hidden-import other
在目錄結構:「程序根目錄distmain」 下可以找到生成的main.exe。將其他依賴文件拷貝進入「程序根目錄distmain」 下,即可運行。
② python 用循環創建多個文件
Python編程中用for()循環創建多個文件,代碼如下:
#coding=utf-8
'''
Createdon2015-07-05
'''
importos
importtime
defnsfile(s):
''''''
#判斷文件夾是否存在,如果不存在則創建
b=os.path.exists("E:\testFile\")
ifb:
print"FileExist!"
else:
os.mkdir("E:\testFile\")
#生成文件
foriinrange(1,s+1):
localTime=time.strftime("%Y%m%d%H%M%S",time.localtime())
#printlocaltime
filename="E:\testFile\"+localTime+".txt"
#a:以追加模式打開(必要時可以創建)append;b:表示二進制
f=open(filename,'ab')
testnote='測試文件'
f.write(testnote)
f.close()
#輸出第幾個文件和對應的文件名稱
print"file"+""+str(i)+":"+str(localTime)+".txt"
time.sleep(1)
print"ALLDown"
time.sleep(1)
if__name__=='__main__':
s=input("請輸入需要生成的文件數:")
nsfile(s)
③ python創建根據時間的txt文件
importtime
tm=time.strftime("%Y-%m-%d%X",time.localtime())
timeslog=tm+r'.txt'
sp=open(timeslog,'w')
sp.close()
代碼沒問題
但是Windows系統中 文件名不能包含下列任何字元:
/ : * ? 」(英文右引號) < > |
所以考慮下修改下時間格式
④ 請教各位如何用python創建文件和文件夾
python創建文件
>>>f=open('f.txt','w')#r只讀,w可寫,a追加
>>>foriinrange(0,10):f.write(str(i)+' ')
>>>f.close()
python創建文件夾
importos
os.makedirs("目錄")
如果解決了您的問題請採納!
如果未解決請繼續追問!