Question: PYTHON I am having trouble with the user_prompt() portion of the program. When ever I enter a new name it just keeps asking me to
PYTHON
I am having trouble with the user_prompt() portion of the program. When ever I enter a new name it just keeps asking me to re-enter a new name. Below are is the parameters of the program and my code as well, any help would be awesome!
A good friend of yours has sent you a zip folder containing various images. He asked if you could write a program that will automatically unzip the file and rename all the files in the unzipped folder by using a prefix and a number. He would then like the folder re-named and zipped
The program will prompt the user the name of the zip file in the current folder to unzip, the desired prefix of the files within the folder to be re-named and the starting index for the files being re-named.
Example prompts:Enter your zip file name (xxxx.zip):Images.zipEnter the desired prefix for renaming your files:Img-Enter the starting number (integer) of your files to be named:1
From the example above, the specified my zip files was Images.zip. The files within unzipped file will be renamed with the prefix Img- The starting number for the files will be: 1
So, for this example, the Images.zip file will unzip to Images.All the files within the Images folder will be renamed: Img-1.xxx, Img-2.xxx,etc. where xxx is the file suffix.The program will next rename the Images folder to new_Images and zip the folder.
The program will need to make use of the following functions:main(), user_prompt(), zip(), and unzip(). The main() function can be broken down in in some smaller functions if desired.
In the main() function, the program should check to see if the zip file name, the file prefix and the starting numbers were passed to the program. If they were, the program can use that information to be passed to user_prompt() function which will do validation on the input passed to it.
The user_prompt() function will validate each of the 3 pieces information passed to it by looping on each prompt to the user until the information is valid.
Input File Name Checks:Input file names must be greater than 3 characters. Input file names provided must be checked if the file exists in the current folder.
Prefix Check- The prefix provided must be 3 or more characters
Starting Number Check: The starting number must be a positive integer.
The program will be using the following libraries: os, sys, shutil, zipfile
When zipping the folder, the program should use the compression=zipfile.ZIP_DEFLATED option.
import os import zipfile import shutil
def main(): #Prompt the use to enter the Zip file Name.We use the input() function to prompt the user zipFileName = input('Please enter your zip file name (xxxx.zip): ') #Prompt the use to enter the prefix.We use the input() function to prompt the user prefixText = input('Enter the desired prefix for renaming your files: ') #Prompt the use to enter the starting number.We use the input() function to prompt the user startNumber = input('Enter the starting number (integer) of your files to be named: ') #Call the user_prompt() for validation validInputList = user_prompt(zipFileName, prefixText, startNumber) dirpath = os.path.abspath(validInputList[0].replace(".zip", "")) newFileName = "new_" + validInputList[0] newFilePath = os.path.abspath(newFileName.replace(".zip", "")) if os.path.isdir(dirpath): shutil.rmtree(dirpath) if os.path.isdir(newFilePath): shutil.rmtree(newFilePath) unzip(validInputList) Zip(validInputList) #The user_prompt() function validate the inputs entered by the user def user_prompt(zipFileName, prefixText, startNumber): #Call the isZipFileNameInValid function to check if the entered input is Valid while (isZipFileNameInValid(zipFileName)): zipFileName = input('Please enter the zip file name (xxxx.zip). File Name must be more than three characters and exist in current directory: ') #Call the isprefixTextInValid() function to check if the entered input is Valid while (isprefixTextInValid(prefixText)): prefixText = input('Enter the desired prefix for renaming your files.The prefix provided must be 3 or more characters: ') #Call the isStartNumberInvalid() function to check if the entered input is Valid while(isStartNumberInvalid(startNumber)): startNumber = input('Enter the starting number (integer) of your files to be named.The starting number must be a positive integer: ') return [zipFileName, prefixText, startNumber]
#The isZipFileNameInValid function checks the input entered by the user and returns True if a INVALID input is given #And returns False if a VALID input is given def isZipFileNameInValid(zipFileName): #The path where the python program is executed from progFilepath = os.path.dirname(os.path.abspath(__file__)) #Set the working directory to the place from where the python program is present os.chdir(progFilepath) #Check if there is a extension in the input file name if zipFileName.count(".") != 1: return True #Split the input as fileName and extension fileList = zipFileName.split(".") zipFileNameTxt =fileList[0] zipFileExtension =fileList[1] #Check if the extension is a vaid ZIP extension. if zipFileExtension.upper() != 'ZIP': return True #Check if the length of the zipFileName is greater than 3 Characters. #If the file Name is less than or equal to 3 characters the functions returns TRUE. #Return of TRUE value indicates the input entered by the user is invalid if (len(zipFileNameTxt) <= 3): return True #If the file Name does not contains aplhabets the functions returns TRUE #Return of TRUE value indicates the input entered by the user is invalid if zipFileNameTxt.isalpha() == False: return True #If the file does not exist in the current path the function returns TRUE #Return of TRUE value indicates the input entered by the user is invalid fullFileName = zipFileNameTxt + "." + zipFileExtension if (os.path.exists(fullFileName) == False): return True #If the control reaches the below statement means all input by the user is VALID #So the function returns FALSE. return False def isprefixTextInValid(prefixText): #Check if the length of the prefix text is greater than oe equal to 3 Characters. #If the prefix text is less than 3 characters the functions returns TRUE. if (len(prefixText) < 3): return True #If the file Name does not contains aplhabets the functions returns TRUE #Return of TRUE value indicates the input entered by the user is invalid #Return of TRUE value indicates the input entered by the user is invalid if prefixText.isalpha() == False: return True #If the control reaches the below statement means all input by the user is VALID #So the function returns FALSE. return False
def isStartNumberInvalid(startNumber): #If the file Name does not contains digits the functions returns TRUE #Return of TRUE value indicates the input entered by the user is invalid if startNumber.isdigit() == False: return True #Check if the input values is a positive Value.If not functions returns TRUE #Return of TRUE value indicates the input entered by the user is invalid if (int(startNumber) < 0 ): return True #If the control reaches the below statement means all input by the user is VALID #So the function returns FALSE. return False def zipdir(zippath, ziph): # ziph is zipfile handle for root, dirs, files in os.walk(zippath): for file in files: ziph.write(os.path.join(root, file)) def Zip(userInputValues): zipFileName = userInputValues[0] fullPath = os.path.abspath(zipFileName.replace(".zip", "")) newFilePath = "new_" + zipFileName.replace(".zip", "") newZipFileName = newFilePath + ".zip" if os.path.exists(newZipFileName): os.remove(newZipFileName) os.rename(os.path.join(fullPath), os.path.join(newFilePath)) zipf = zipfile.ZipFile(newZipFileName, 'w', zipfile.ZIP_DEFLATED) zipdir(newFilePath , zipf) zipf.close()
def unzip(userInputValues): zipFileName = userInputValues[0] prefix = userInputValues[1] numberSequence = userInputValues[2] # Create a ZipFile Object and load sample.zip in it with zipfile.ZipFile(zipFileName, 'r') as zipObj: # Extract all the contents of zip file in current directory zipObj.extractall(zipFileName.replace(".zip", "")) #Rename all the file to the required format fullPath = os.path.abspath(zipFileName.replace(".zip", "")) files = os.listdir(fullPath) renamedFileName = "" for index, file in enumerate(files): filename, file_extension = os.path.splitext(file) renamedFileName = prefix + str(numberSequence) + file_extension os.rename(os.path.join(fullPath, file), os.path.join(fullPath, renamedFileName)) numberSequence = str(int(numberSequence) + 1)
if __name__ == '__main__': main()
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
