我想通过FTP发送一个zip文件。我有一个.cmd可以工作,但我的问题是python在压缩文件之前触发.cmd,所以没有任何东西被发送。这是我的代码。
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文件,因此没有发送任何内容。 我用Google搜索并找到.call和.wait的子进程。但是,我不明白它们是如何运作的,并且无法找到我可以修改和使用它们的例子。谢谢!
答案 0 :(得分:1)
为什么不等待所有文件被压缩然后移动?我不确定你的.cmd文件是做什么的。
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)