views:

899

answers:

5

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.

+2  A: 

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.

John Millikin
A: 
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].

William Keller
You can actually refer to the mtime like this: os.stat('test').st_mtime - it removes the need to import the stat module.
Jerub
+1  A: 

I wrote a proof-of-concept for a batch command processor in Python, but haven't had the chance to finish it. You may find it useful.

Ignacio Vazquez-Abrams
+5  A: 
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))
wnoise
To just get the mtime of a file, use os.path.getmtime(fullname) rather than os.stat(fullname).st_mtime
Thomas Wouters
Thanks! Incorporated.
wnoise
You can use shutil.move to do the move. It default to os.rename if source and destination are on the same filesystem, or do a copy/delete.
Sylvain Defresne
os.walk is very nice!
Warren P
+2  A: 
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.

nosklo
You don't need the backslash in front of the ';' in Python.
Glyph
@Glyph: you are correct. Luckily for nosklo, the backslash will be consumed (effectively ignored in this case) by python.
ΤΖΩΤΖΙΟΥ
Thanks for the info. Dropping it.
nosklo