Advertisement
ade_talon

Matt's Backup Program

Mar 24th, 2022
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. import shutil, zipfile,os
  2.  
  3. def main():
  4. os.system('clear')
  5.  
  6. #main menu
  7. COMPRESS = 1
  8. DECOMP = 2
  9. QUIT = 0
  10.  
  11. choice = 99
  12.  
  13. while choice != QUIT:
  14. menu()
  15.  
  16. try:
  17. CHOICE = int(input("\nSelect an option: "))
  18.  
  19. if CHOICE == COMPRESS:
  20. #inialize file compression function
  21. fileCompression()
  22.  
  23. elif CHOICE == DECOMP:
  24. #inialize file decompression function
  25. fileDeCompression()
  26.  
  27. elif CHOICE == QUIT:
  28. #quit
  29. print("Bye for now!")
  30. break
  31.  
  32. else:
  33. #handle if user doesn't input a valid option
  34. print("\nPlease enter a valid option (1,2 and 0)")
  35.  
  36. #error handling if user inputs a non integer
  37. except ValueError as err:
  38. print("\nEnter an integer (1,2 and 0)")
  39. print(err)
  40.  
  41.  
  42.  
  43.  
  44. def fileCompression():
  45. os.system('clear')
  46. print(" CREATE A COMPRESSED FILE")
  47. print("---------------------------------")
  48. print("\nnote: the only working compression file type is .zip - file will automatically be saved as a zip file")
  49.  
  50. #take in information
  51. new_directory_name = input("\nPlease name your compressed file: ")
  52. archive_from = input("\nPlease enter the directory (and path) you want to archive: ")
  53.  
  54. #user input for number of copies
  55. backup_count = int(input("How many copies do you want to create?: "))
  56.  
  57. print("\nEnter a directory (and path) you want to save your compressed file to:")
  58. new_directory = input("Path: ")
  59.  
  60. #add forward slash to end of directory if user doesn't add a slash
  61. if new_directory[-1] != '/':
  62. new_directory = new_directory + '/'
  63.  
  64. #loop through number of copies
  65. for num in range(backup_count):
  66. #if iteration is equal to 0, do not add numeric addition to title
  67. if num == 0:
  68. zippedFile = shutil.make_archive(f"{new_directory}{new_directory_name}", 'zip', archive_from)
  69. print("Zipped File Created!")
  70.  
  71. #add numeric ending so multiple files can be created
  72. else:
  73. zippedFile = shutil.make_archive(f"{new_directory}{new_directory_name}_{str(num)}", 'zip', archive_from)
  74. print("Zipped File Created!")
  75.  
  76. print("\nJob Completed.")
  77.  
  78. #continue?
  79. choice = input("Compress another file (y/n)?: ").lower()
  80.  
  81. if choice == "y":
  82. fileCompression()
  83.  
  84. else:
  85. main()
  86.  
  87.  
  88. def fileDeCompression():
  89. os.system('clear')
  90. print(" DECOMPRESS A FILE")
  91. print("--------------------------------")
  92.  
  93. #take in information
  94. print("\nnote: the only working compression file type is .zip")
  95. file_name = input("\nEnter file you want to unpack (including file extension and directory path): ")
  96.  
  97. #add file extension if user forgets
  98. if ".zip" not in file_name:
  99. file_name = file_name + ".zip"
  100.  
  101. extract_dir = input(f"\nEnter directory and path that you want to extract to (leave blank to use current directory: {os.getcwd()}): ")
  102.  
  103. #extract content from desired file
  104. shutil.unpack_archive(file_name,extract_dir,'zip')
  105.  
  106. print("File has been unpacked!")
  107.  
  108. #continue?
  109. choice = input("Do you want to delete the compressed file (y/n)?: ").lower()
  110. if choice == 'y':
  111. os.remove(file_name)
  112. choice = input("Do you want to decompress another file (y/n)?: ").lower()
  113.  
  114. if choice == 'y':
  115. fileDeCompression()
  116.  
  117. else:
  118. main()
  119.  
  120. else:
  121. choice = input("Do you want to decompress another file(y/n)?: ").lower()
  122.  
  123. if choice == 'y':
  124. fileDeCompression()
  125.  
  126. else:
  127. main()
  128.  
  129.  
  130.  
  131. def menu():
  132. #main menu screen
  133. print(" File Compression Application")
  134. print("-----------------------------------------")
  135.  
  136. print("MENU")
  137. print("1. Compress a directory")
  138. print("2. Decompress an existing file")
  139. print("0. QUIT")
  140.  
  141. if __name__ == '__main__':
  142. main()
  143.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement