1

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.

dreftymac
  • 31,404
  • 26
  • 119
  • 182
unmounted
  • 33,530
  • 16
  • 61
  • 61

4 Answers4

5
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
  • 9,764
  • 37
  • 47
3
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
  • 217,122
  • 57
  • 293
  • 297
  • You don't need the backslash in front of the ';' in Python. – Glyph Sep 23 '08 at 04:35
  • @Glyph: you are correct. Luckily for nosklo, the backslash will be consumed (effectively ignored in this case) by python. – tzot Sep 23 '08 at 09:38
2

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
  • 197,344
  • 39
  • 212
  • 226
0
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
  • 5,256
  • 1
  • 25
  • 22
  • You can actually refer to the mtime like this: os.stat('test').st_mtime - it removes the need to import the stat module. – Jerub Sep 23 '08 at 02:19