The goal of the assignment is to add functionality to the menu which will restore the archive to a location on your computer which you specify. In terms of the menu, three items are necessary.
1. Functionality which permits the user to enter the path on the computer which points to the directory where you want the restoration done.2. Functionality which restores the archive to the location specified in 1 above.3. Functionality which permits the user to specify, from a list in the archival restoration, but just a few of the files.I already have a python txt file with other function. I also have a menu in the txt file. so I want these 3 items to be in my original menu. My computer is a MacBook and I am using WinZip to unzip files. The program has to work on MacBook and WinZip.
# -*- coding: utf-8 -*- """ Created on Tue Mar 2 14:01:04 2021 @author: pjh """ import subprocess as sp import os import sys # ============================================================================ # Startup code to establish platform so functions will work correctly # ============================================================================ PLAT = "UNKNOWN" platform = sys.platform if platform == "win32": PLAT = "WIN" elif platform == "darwin": PLAT = "MAC" else: print("Cannot establish operating system platform:", platform) sys.exit(0) # ============================================================================= def getDiskFileList(dirPath): if not os.path.isdir(startDir): print("ERROR: Starting file directory path not found") return None fl = list() for root, directories, files in os.walk(dirPath, topdown=False): for name in files: fl.append(os.path.join(root, name)) return fl # ============================================================================= def getArchiveFileList(dirPath, zipPath): from zipfile import ZipFile if not os.path.isfile(zipPath): print("ERROR: Archive file not found") return None with ZipFile(outArchive, "r") as zipF: nList = zipF.namelist() return nList # ============================================================================= def buildArchive(srceDirPath, archivePath, exe = None): print("...This may take a few minutes...") if PLAT == "WIN": args = [exe, "-arP", archivePath, srceDirPath + os.sep + "*.*"] elif PLAT == "MAC": args = ["zip", "-r", archivePath, srceDirPath] stdout = sp.run(args, text = True, capture_output = True).stdout return stdout # ============================================================================== def updateArchive(srceDirPath, archivePath, exe = None): print("...This may take a few minutes...") if PLAT == "WIN": args = [exe, "-urP", archivePath, srceDirPath + os.sep + "*.*"] elif PLAT == "MAC": args = ["zip", "-ur", archivePath, srceDirPath] stdout = sp.run(args, text = True, capture_output = True).stdout return stdout # ============================================================================= def delArchive(archivePath): if not os.path.isfile(outArchive): print("ERROR:", "Archive file not found") return False else: os.remove(outArchive) return True # ============================================================================= def findMatchOnDisk(aList, fList, idx): aa = aList[idx].replace("\\", "/") #This does no harm on mac for f in fList: #Seek a match in file list pos = f.find(aa) if pos > -1: return True return False # ============================================================================= def findMatchInArchive(aList, fList, idx): dFile = fList[idx] if PLAT == "WIN": dFile = dFile[3:] dFile = dFile.replace("\\", "/") elif PLAT == "MAC": dFile = dFile[1:] for a in aList: #Seek a match in archive list pos = a.find(dFile) if pos > -1: return True return False # ============================================================================= def removeOrphansFromArchive(startDir, archivePath, exe, confirmDel = False): aL = getArchiveFileList(startDir, archivePath) fL = getDiskFileList(startDir) if PLAT == "MAC": exe = "zip" for i in range(0, len(aL)): matched = findMatchOnDisk(aL, fL, i) if not matched: #We went through all the f and not on disk, args = [exe, "-d", archivePath, aL[i]] #so delete it in archive if confirmDel: stdout = sp.run(args, text = True, capture_output = True).stdout print(stdout) else: print("Simulated deletion from archive:", aL[i]) print(" ", args) return True # ============================================================================= def printDiskFiles(startDir): fL = getDiskFileList(startDir) print() print("Disk file paths:") for fileName in fL: print("\t",fileName) # ============================================================================= def printArchiveFiles(dirPath, archivePath): aList = getArchiveFileList(dirPath, archivePath) print() print("File paths in archive:") for fileName in aList: print("\t",fileName) # ============================================================================= def printDifferences(dirPath, archivePath): aL = getArchiveFileList(startDir, archivePath) fL = getDiskFileList(startDir) matchers = list() print("In archive but not on disk:") for i in range(0, len(aL)): matched = findMatchOnDisk(aL, fL, i) if not matched: #In archive but not on disk print(" ", aL[i]) else: matchers.append(aL[i]) print("On disk but not in archive:") for i in range(0, len(fL)): matched = findMatchInArchive(aL, fL, i) if not matched: print(" ", fL[i]) print("On both disk and in archive:") for p in matchers: print(" ", p) # ============================================================================= # Main Program PC and Mac version # ============================================================================= if PLAT == "WIN": exe = "C:\\Program Files\\WinZip\\wzzip.exe" startDir = r"E:\Images" outArchive = r"E:\TIFMaps.zip" elif PLAT == "MAC": exe = None startDir = "/Volumes/usb31fd/Images" outArchive = "/Volumes/usb31fd/TIFMaps.zip" else: print("Cannot establish operating system platform:", PLAT) sys.exit(0) #This is all platform independent while True: print() print("What do you want to do?") print("1: Build or rebuild archive.") print("2: Update archive." ) print("3: Delete archive.") print("4: List the files on disk") print("5: List the files in the archive") print("6: Compare file names on disk and archive") print("7: Cull the archive for files removed from hard drive") print("9: End program") sel = input("Enter menu choice: ") if (sel == "9"): sys.exit(0) elif(sel == "1"): msg = buildArchive(startDir, outArchive, exe) print(msg) elif (sel == "2"): msg = updateArchive(startDir, outArchive, exe) print(msg) elif (sel == "3"): ok = delArchive(outArchive) if ok: print("Archive file deleted.") elif (sel == "4"): printDiskFiles(startDir) elif (sel == "5"): printArchiveFiles(startDir, outArchive) elif (sel == "6"): printDifferences(startDir, outArchive) pass elif (sel == "7"): removeOrphansFromArchive(startDir, outArchive, exe) else: continue