我想通過FTP發送zip文件。我有一個.cmd工作,但我的問題是python在文件壓縮之前觸發.cmd,因此沒有任何內容被髮送。這是我的代碼。Python,發送之前先等待文件壓縮
Source = (r"D:\Backup\test2") #where the files originate
Destination = (r"D:\Backup\ZipFilesToMove") #where they move to
SendZipfiles = (['C:\BackupFiles\RichCopyControls.cmd']) #the .cmd file
from os import listdir
from os.path import isfile, join
onlyfiles = [ f for f in listdir(Source) if isfile(join(Source,f)) ] v#get each file in folder
Amount = len(onlyfiles) #how many files are in folder
Counter = 0
lst = onlyfiles #give the list the name lst
while(Counter < Amount):
zf = zipfile.ZipFile(lst[Counter],"w", zipfile.ZIP_DEFLATED,allowZip64=True) # create zip
zf.write(os.path.join(lst[Counter])) #zip it up
zf.close() #close the zip
shutil.move(os.path.join(lst[Counter]),Destination) #move to zip folder
p = subprocess.Popen(SendZipfiles) #the problem is here I think, this is where it runs the .cmd
程序的其餘部分工作正常,問題是.cmd文件被打開,並完成之前,這些文件都拉鍊所以沒有被髮送。 我搜索了一下,發現了.call和.wait的子進程。但是,我不明白他們如何運作,並找不到我可以修改和使用的例子。謝謝!
只是一個樣式說明。標題大小通常保留給類,而不是變量。這可能會讓其他人閱讀你的代碼時感到困惑。 – Joe
我會記得下次感謝你。 – vmedhe2