Task1 : Write a python script to generate 50 files for each of the following thr
ID: 3817396 • Letter: T
Question
Task1: Write a python script to generate 50 files for each of the following three courses:
IT151
IT421
IT620
with the following criteria:
- 25 random dates between 0 and 24 days ago.
- 25 random dates between 25 and 50 days ago.
In the format,
IT421_unixdate.txt
IT151_unixdate.txt
IT620_unixdate.txt
where you replace unixdate with the UNIX time stamp.
An example of a file that may be generated by your script: IT421_1456991403.txt.
Task2: Write a python script to:
Check if the folders IT421 , IT151 , and IT620 exist. If they do not, create them in your folder.
Use libraries: os, ex: os.mkdir()
Then move the files that begin with ITx to the folder ITx where x is the class number. If the file already exists, rename it like the following example:
IT421_1456991403.txt becomes IT421_1456991403 (1).txt
And, IT421_1456991403 (1).txt becomes IT421_1456991403 (2).txt
Note that if you do not have duplicates, copy a few into another folder and test your code.
Task3: Write a python script to:
For each folder, create a folder called old if it does not exist. Move all files older than 25 days into that folder.
Apply the same renaming scheme as done in task 2.
Task4: Write a python script to:
Write a python script to, using the date from the filename, display:
-Oldest file
-Newest file
-How much time has elapsed between the newest and oldest file.
Display in days, hours, minutes, and seconds. Use datetime.timediff for each of the three folders.
Explanation / Answer
""" Author: Date :7/3/17 File Name:file_generation.py Description: """ from datetime import datetime, timedelta import random import os def generate_random_date(current_date, number_of_date, date_range=None): """ This function take three parameter as input and return the list of date with the given length (number_of_date) and range. :type current_date:datetime :type number_of_date:int :type date_range: list :param current_date: this is current system date :param number_of_date: how many dates you want :param date_range: this is list and it have two value lower and upper bound like [0,24] :return: list of dates(unix timestamp) """ date_list = [] random_days_before = [] while len(random_days_before) != number_of_date: days_before = random.randint(*date_range) if days_before not in random_days_before: random_days_before.append(days_before) print(random_days_before) for i in range(number_of_date): date_list.append(int((current_date.utcnow() - timedelta(random_days_before[i])).timestamp())) return date_list def create_file(list_of_folder_name, date_list): """ This function create files with given prefix :param list_of_folder_name: :param date_list: :return: """ for folder_name in list_of_folder_name: for date in date_list: file_name = folder_name + "_" + str(date) + ".txt" f = open("./" + file_name, "w") f.close() if __name__ == '__main__': courses = ["IT151", "IT421", "IT620"] c_date = datetime.now() dates_type_1 = generate_random_date(c_date, 25, [0, 24]) dates_type_2 = generate_random_date(c_date, 25, [25, 50]) create_file(courses, dates_type_1) create_file(courses, dates_type_2) """ Author: Date :7/3/17 File Name:task2.py Description: """ import os def create_folder(folder_names): for directory in folder_names: if not os.path.exists(directory): os.mkdir(directory) for directory in folder_names: dir_list = os.listdir("./") print(dir_list) for file in dir_list: if file.startswith(directory): destination_path = directory + "/" + file count = 1 while os.path.exists(destination_path): name, ext = os.path.splitext(file) destination_path = directory + "/" + name + "(" + str(count) + ")" + ext count += 1 print(destination_path) os.rename(file, destination_path) # if os.path.exists(directory+"") if __name__ == '__main__': courses = ["IT151", "IT421", "IT620"] create_folder(courses) """ Author: Date :7/3/17 File Name:task3.py Description: """ import os from datetime import datetime def make_old_dir(folder_names): for directory in folder_names: old_dir_name = directory + "_old" if not os.path.exists(old_dir_name): os.mkdir(old_dir_name) for directory in folder_names: dir_list = os.listdir(directory) for file in dir_list: utc_time = file.split("_")[1].split(".")[0].split("(")[0] diff = datetime.now() - datetime.fromtimestamp(int(utc_time)) if diff.days > 25: destination_path = directory + "_old" + "/" + file count = 1 while os.path.exists(destination_path): name, ext = os.path.splitext(file) destination_path = directory + "/" + name + "(" + str(count) + ")" + ext count += 1 print(destination_path) os.rename(directory + "/" + file, destination_path) if __name__ == '__main__': courses = ["IT151", "IT421", "IT620"] make_old_dir(courses) """ Author: Date :7/3/17 File Name:task4.py Description: """ import os from datetime import datetime def get_oldest_file(folder_names): oldest = -1 days = -1 oldest_file = '' for directory in folder_names: dir_list = os.listdir(directory) for file in dir_list: utc_time = file.split("_")[1].split(".")[0].split("(")[0] diff = datetime.now() - datetime.fromtimestamp(int(utc_time)) if diff.days > days: days = diff.days oldest = int(utc_time) oldest_file = file return oldest, oldest_file def get_newest_file(folder_names): days = datetime.now() newest_file = [] min_dates = [] for directory in folder_names: dir_list = os.listdir(directory) for file in dir_list: utc_time = file.split("_")[1].split(".")[0].split("(")[0] diff = datetime.fromtimestamp(int(utc_time)) if diffRelated Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.