Task: Moving MP3 Files Based on Metadata Date
import os
import shutil
from datetime import datetime
def list_files_in_folder(folder_path):
return os.listdir(folder_path)
def get_file_format():
return input("Enter the file format (e.g., .mp3, .jpg): ")
def get_creation_date(file_path):
return datetime.fromtimestamp(os.path.getctime(file_path))
def get_user_date():
date_str = input("Enter the date (YYYY-MM-DD): ")
return datetime.strptime(date_str, '%Y-%m-%d')
def move_files_based_on_date(folder_path, file_format, user_date, destination_folder):
if not os.path.exists(destination_folder):
os.makedirs(destination_folder)
for file_name in list_files_in_folder(folder_path):
if file_name.endswith(file_format):
file_path = os.path.join(folder_path, file_name)
creation_date = get_creation_date(file_path)
if creation_date.date() == user_date.date():
shutil.move(file_path, os.path.join(destination_folder, file_name))
print(f"Moved: {file_name}")
def main():
folder_path = ("/home/sakthivel/Documents/Practice/task")
destination_folder = ("/home/sakthivel/Documents/Practice/mp3")
if not os.path.exists(folder_path):
print("Folder does not exist.")
return
file_format = get_file_format()
user_date = get_user_date()
move_files_based_on_date(folder_path, file_format, user_date, destination_folder)
if __name__ == "__main__":
main()
Detailed Definition:
This Python script automates the task of moving files from one directory to another based on their creation date. The script follows these main steps:
- List Files in a Folder:
- Function:
list_files_in_folder(folder_path)
- Description: This function takes a folder path as an argument and returns a list of all files in that folder.
- Function:
- Get File Format from User:
- Function:
get_file_format()
- Description: This function prompts the user to enter a file format (e.g.,
.mp3
,.jpg
). The entered format is returned as a string.
- Function:
- Get Creation Date of a File:
- Function:
get_creation_date(file_path)
- Description: This function takes the file path as an argument and returns the creation date of the file as a
datetime
object.
- Function:
- Get Date from User:
- Function:
get_user_date()
- Description: This function prompts the user to enter a date in the format
YYYY-MM-DD
. The entered date is converted to adatetime
object and returned.
- Function:
- Move Files Based on Date:
- Function:
move_files_based_on_date(folder_path, file_format, user_date, destination_folder)
- Description: This function moves files from the source folder to the destination folder based on the specified file format and user-provided date.
- It first checks if the destination folder exists; if not, it creates it.
- It then iterates over the files in the source folder, checking if each file matches the specified format and creation date.
- If a match is found, the file is moved to the destination folder, and a message is printed indicating the file has been moved.
- Function:
- Main Function:
- Function:
main()
- Description: This is the entry point of the script. It sets the paths for the source and destination folders and performs the following steps:
- Verifies the existence of the source folder.
- Retrieves the file format and date from the user.
- Calls the function to move files based on the provided criteria.
- Function:
- Script Execution:
- The script is executed by calling the
main()
function when the script is run directly.
- The script is executed by calling the
Enhancements for Future Consideration:
- User Input Validation: Ensure the file format and date inputs are valid.
- Error Handling: Implement error handling for file operations and user inputs.
- Logging: Add logging to keep track of the operations performed and any errors encountered.
- Flexible Date Comparison: Allow for more flexible date comparisons, such as moving files created on or after a specified date.
By following these steps, the script efficiently organizes files based on their creation dates, making it a useful tool for managing large collections of files.
Output: