Skip to content

Instantly share code, notes, and snippets.

@iamtalhaasghar
Created January 13, 2021 09:58
Show Gist options
  • Save iamtalhaasghar/e5650928b41b4bdd4c9b4df36b14d559 to your computer and use it in GitHub Desktop.
Save iamtalhaasghar/e5650928b41b4bdd4c9b4df36b14d559 to your computer and use it in GitHub Desktop.
A python module which creates .zip & .rar archives of subfolders of a given path
# A python module which creates .zip & .rar archives of subfolders of a given path
def subFolders(folder_path):
'Returns list of subfolders of given path'
import os
foldersList = list()
print('Looking for subfolder of : %s' % (folder_path))
for i in os.listdir(folder_path):
if(os.path.isdir(i)):
print('%s' % (i), end=', ')
foldersList.append(i)
print('\nTotal Subfolders: %s' % (len(foldersList)))
return foldersList
def createArchives(pathList, extension = 'zip'):
'create archives of folders list passed in first argument'
import os
for f in pathList:
command = 'winrar.exe a "%s.%s" "%s" ' % (f, extension, f)
#print(command)
print('Creating Archive: "%s" >>> "%s.%s"' % (f, f, extension))
os.system(command)
print('Success: I am free now!')
if __name__ == "__main__":
import sys
import os
print('# Zipper by Talha Asghar')
if(len(sys.argv) == 1):
choice = input('Proceed with "%s" \n(y=yes, n=no) ? : ' % os.getcwd())
if(choice == 'Y' or choice == 'y'):
createArchives(subFolders(os.getcwd()))
elif (len(sys.argv) == 2):
choice = input('Proceed with "%s" \n(y=yes, n=no) ? : ' % os.getcwd())
if(choice == 'Y' or choice == 'y'):
createArchives(subFolders(os.getcwd()), sys.argv[1])
else:
print('Error : Invalid Command Entered!\nUsage : zipper.py [,extension]')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment