Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0613· OpenAIAi2 WildChat-4.8M (AI2)

Can you create me a python3 script that archives all mp4 files in an identified folder…

  1. User

    Can you create me a python3 script that archives all mp4 files in an identified folder created the previous day and deletes the oldest zip archives from the same folder when the Linux system reaches less than 1GB of storage. This script should run uninterrupted as a Linux system service.

  2. Assistant

    Sure! Here's a Python3 script that archives all MP4 files created the previous day in a specified folder, and deletes the oldest zip archives from the same folder when the Linux system reaches less than 1GB of storage. You can run this script as a Linux system service using systemd.

    Please note that this script assumes you have the required permissions to access and modify files and folders on your Linux system.

    import os
    import shutil
    import datetime
    
    # Configurations
    folder_path = '/path/to/folder'  # Replace with the actual folder path
    storage_limit = 1 * 1024 * 1024 * 1024  # 1GB
    
    # Calculate date of previous day
    previous_day = datetime.datetime.now() - datetime.timedelta(days=1)
    previous_day_str = previous_day.strftime('%Y-%m-%d')
    
    # Create archive folder if it doesn't exist
    archive_folder = os.path.join(folder_path, 'archive')
    os.makedirs(archive_folder, exist_ok=True)
    
    # Archive MP4 files created on the previous day
    for filename in os.listdir(folder_path):
        if filename.endswith('.mp4'):
            file_path = os.path.join(folder_path, filename)
            file_creation_date = datetime.datetime.fromtimestamp(os.path.getctime(file_path))
            if file_creation_date.strftime('%Y-%m-%d') == previous_day_str:
                shutil.move(file_path, archive_folder)  # Move to archive folder
    
    # Calculate current system storage size
    statvfs = os.statvfs('/')
    free_space = statvfs.f_frsize * statvfs.f_bavail
    
    # Delete oldest zip archives when storage is less than 1GB
    if free_space < storage_limit:
        # Sort zip archives by creation time (oldest first)
        sorted_archives = sorted(os.listdir(archive_folder), key=lambda x: os.path.getctime(os.path.join(archive_folder, x)))
    
        while free_space < storage_limit and sorted_archives:
            oldest_archive = sorted_archives.pop(0)
            archive_path = os.path.join(archive_folder, oldest_archive)
            file_size = os.path.getsize(archive_path)
            os.remove(archive_path)  # Delete oldest archive
            free_space += file_size
    
        print('Deleted oldest zip archives to maintain enough storage.')
    
    print('Script execution complete.')
    

    To run this script as a Linux system service, you can create a systemd unit file. Follow these steps:

    1. Create and edit a unit file using the command: sudo nano /etc/systemd/system/mp4_archiver.service
    2. Add the following contents to the unit file:
    [Unit]
    Description=MP4 Archiver Service
    
    [Service]
    ExecStart=/usr/bin/python3 /path/to/script.py  # Replace with the actual script path
    Restart=always
    
    [Install]
    WantedBy=multi-user.target
    
    1. Save and close the unit file.
    2. Reload the systemd daemon: sudo systemctl daemon-reload
    3. Start the service: sudo systemctl start mp4_archiver
    4. Enable automatic startup of the service on boot: sudo systemctl enable mp4_archiver

    Your script will now run as a system service and archive/delete files according to the specified logic.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.