zipfile program
This commit is contained in:
		
							parent
							
								
									81fe349a23
								
							
						
					
					
						commit
						e8375cfc65
					
				
					 1 changed files with 36 additions and 0 deletions
				
			
		
							
								
								
									
										36
									
								
								backupToZip.py
									
										
									
									
									
										Executable file
									
								
							
							
						
						
									
										36
									
								
								backupToZip.py
									
										
									
									
									
										Executable file
									
								
							| 
						 | 
					@ -0,0 +1,36 @@
 | 
				
			||||||
 | 
					#! /usr/bin/env python3
 | 
				
			||||||
 | 
					usage = "backupToZip: usage backupToZip [directory] backup given directory to a zip file, incrementing filename if name already exists"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import zipfile, os, sys
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if len(sys.argv)<1:
 | 
				
			||||||
 | 
					    print(usage)
 | 
				
			||||||
 | 
					    sys.exit()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					dir = os.path.realpath(sys.argv[1])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#figure out filename based on what already exists.
 | 
				
			||||||
 | 
					number = 1
 | 
				
			||||||
 | 
					while True:
 | 
				
			||||||
 | 
					    zipFilename = os.path.basename(dir)+'-'+str(number)+'.zip'
 | 
				
			||||||
 | 
					    if not os.path.exists(zipFilename):
 | 
				
			||||||
 | 
					        break
 | 
				
			||||||
 | 
					    number += 1
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					print(F'Creating {zipFilename}')
 | 
				
			||||||
 | 
					backupZip = zipfile.ZipFile(zipFilename, 'w')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					for dirName, subDirList, fileList in os.walk(dir):
 | 
				
			||||||
 | 
					    #print(f'Adding files in {dirName}')
 | 
				
			||||||
 | 
					    backupZip.write(dirName)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    for fileName in fileList:
 | 
				
			||||||
 | 
					        #lets not backup other backup files.
 | 
				
			||||||
 | 
					        newBase = os.path.basename(dir)+'-'
 | 
				
			||||||
 | 
					        if fileName.startswith(newBase) and fileName.endswith('.zip'):
 | 
				
			||||||
 | 
					            print(f'skipping {fileName}, is backup file.')
 | 
				
			||||||
 | 
					            continue
 | 
				
			||||||
 | 
					        print(f'adding {fileName}')
 | 
				
			||||||
 | 
					        backupZip.write(os.path.join(dirName, fileName))
 | 
				
			||||||
 | 
					backupZip.close()
 | 
				
			||||||
 | 
					print('done')
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue