Level 3: Real-World
HI,
is my version of code below correct?
import csv
import os
import datetime
dir_path = input("Enter the directory path to scan: ")
metadata_list = []
for filename in os.listdir(dir_path):
file_path = os.path.join(dir_path, filename)
if os.path.isfile(file_path):
size = os.path.getsize(file_path)
created_timestamp = os.path.getctime(file_path)
modified_timestamp = os.path.getmtime(file_path)
created_datetime = datetime.datetime.fromtimestamp(created_timestamp)
modified_datetime = datetime.datetime.fromtimestamp(modified_timestamp)
file_type = os.path.splitext(file_path)[1]
print(f"Path: {file_path}, Size: {size}, Created: {created_datetime}, Modified: {modified_datetime}, "
f"Type: {file_type}")
metadata_list.append([file_path, size, created_datetime, modified_datetime, file_type])
save_to_csv = input("Do you want to save the metadata to a CSV file? (Y/N): ")
if save_to_csv.lower() == "y":
csv_filename = "file_metadata_list.csv"
with open(csv_filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Path", "Size (bytes)", "Created", "Modified", "Type"])
writer.writerows(metadata_list)
print(f"File metadata saved to {csv_filename}")
elif save_to_csv.lower() != 'n':
print("Invalid input, File metadata not saved.")
Hi, if it returns the expected output explained on the project page, then yes, it is correct. How was your output?
HI,
is my version of code below correct?
import csv
import os
import datetime
dir_path = input("Enter the directory path to scan: ")
metadata_list = []
for filename in os.listdir(dir_path):
file_path = os.path.join(dir_path, filename)
if os.path.isfile(file_path):
size = os.path.getsize(file_path)
created_timestamp = os.path.getctime(file_path)
modified_timestamp = os.path.getmtime(file_path)
created_datetime = datetime.datetime.fromtimestamp(created_timestamp)
modified_datetime = datetime.datetime.fromtimestamp(modified_timestamp)
file_type = os.path.splitext(file_path)[1]
print(f"Path: {file_path}, Size: {size}, Created: {created_datetime}, Modified: {modified_datetime}, "
f"Type: {file_type}")
metadata_list.append([file_path, size, created_datetime, modified_datetime, file_type])
save_to_csv = input("Do you want to save the metadata to a CSV file? (Y/N): ")
if save_to_csv.lower() == "y":
csv_filename = "file_metadata_list.csv"
with open(csv_filename, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(["Path", "Size (bytes)", "Created", "Modified", "Type"])
writer.writerows(metadata_list)
print(f"File metadata saved to {csv_filename}")
elif save_to_csv.lower() != 'n':
print("Invalid input, File metadata not saved.")
Hi, if it returns the expected output explained on the project page, then yes, it is correct. How was your output?