Reputation: 237
I want to send a zip file via FTP. I have a .cmd that works but my problem is python fires the .cmd before the files are zipped so nothing gets sent. Here is my code.
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
The rest of the program works fine, the problem is the .cmd file is opened and done before the files have zipped so nothing is sent. I have googled around and found subprocesses for .call and .wait. But,I dont understand how they function and cant find an example of them which I can mod and use. Thanks!
Upvotes: 0
Views: 1147
Reputation: 8613
Why not wait for all the files to be zipped and then move? I am not sure what your .cmd file does.
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)
Upvotes: 1