Question

I have this line in a useful Bash script that I haven't managed to translate into Python, where 'a' is a user-input number of days' worth of files to archive:

find ~/podcasts/current -mindepth 2 -mtime '+`a`+' -exec mv {} ~/podcasts/old \;

I am familiar with the os.name and getpass.getuser for the most general cross-platform elements. I also have this function to generate a list of the full names of all the files in the equivalent of ~/podcasts/current:

def AllFiles(filepath, depth=1, flist=[]):
    fpath=os.walk(filepath)
    fpath=[item for item in fpath]
    while depth < len(fpath):
        for item in fpath[depth][-1]:
            flist.append(fpath[depth][0]+os.sep+item)
        depth+=1
    return flist

First off, there must be a better way to do that, any suggestion welcome. Either way, for example, "AllFiles('/users/me/music/itunes/itunes music/podcasts')" gives the relevant list, on Windows. Presumably I should be able to go over this list and call os.stat(list_member).st_mtime and move all the stuff older than a certain number in days to the archive; I am a little stuck on that bit.

Of course, anything with the concision of the bash command would also be illuminating.

Was it helpful?

Solution

import os
import shutil
from os import path
from os.path import join, getmtime
from time import time

archive = "bak"
current = "cur"

def archive_old_versions(days = 3):
    for root, dirs, files in os.walk(current):
        for name in files:
            fullname = join(root, name)
            if (getmtime(fullname) < time() - days * 60 * 60 * 24):
                shutil.move(fullname, join(archive, name))

OTHER TIPS

import subprocess
subprocess.call(['find', '~/podcasts/current', '-mindepth', '2', '-mtime', '+5',
                 '-exec', 'mv', '{}', '~/podcasts/old', ';'], shell=True)

That is not a joke. This python script will do exactly what the bash one does.

EDIT: Dropped the backslash on the last param because it is not needed.

That's not a Bash command, it's a find command. If you really want to port it to Python it's possible, but you'll never be able to write a Python version that's as concise. find has been optimized over 20 years to be excellent at manipulating filesystems, while Python is a general-purpose programming language.

import os, stat
os.stat("test")[stat.ST_MTIME]

Will give you the mtime. I suggest fixing those in walk_results[2], and then recursing, calling the function for each dir in walk_results[1].

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top