views:

197

answers:

3

Currently I have a bash script which runs the find command, like so:

find /storage/disk-1/Media/Video/TV -name *.avi -mtime -7

This gets a list of TV shows that were added to my system in the last 7 days. I then go on to create some symbolic links so I can get to my newest TV shows.

I'm looking to re-code this in Python, but I have a few questions I can seem to find the answers for using Google (maybe I'm not searching for the right thing). I think the best way to sum this up is to ask the question:

How do I perform a search on my file system (should I call find?) which gives me an array of file info objects (containing the modify date, file name, etc) so that I may sort them based on date, and other such things?

+1  A: 
  • You can use "find" through the "subprocess" module.
  • Afterwards, use the "split" string function to dissect each line
  • For each file, use the OS module (e.g. getmtime etc.) to get file information

or

  • Use the "walk" and "glob" modules to get the file paths in objects
jldupont
Running subprocesses when not necessary is inefficient. Glob here isn't efficient either, because it would require double the number of operations on each directory (once for the walk, then a second operation for the glob.)
vy32
I wasn't under the impression that there was that much optimization needed to be done here.
jldupont
+2  A: 

look into module os: os.walk is the function which walks the file system, os.path is the module which gives the file mtime and other file informations. also os.path defines a lot of functions for parsing and splitting filenames.

also of interest, module glob defines a functions for "globbing" strings (matching a string using unix wildcards rules)

from this, building a list of file matching some criterion should be easy.

Adrien Plisson
Pretty much, however, now he needs to ask himself why does he need to code this in python other than "It's cool"
Daniel Goldberg
python is more readable than a bash script and maybe far more efficient and powerful. also, you can build a whole application using python: in his case, he may be looking to mix the searching with a GUI to present the last shows in a user friendly fashion.
Adrien Plisson
He already said this: he now wants to create symbolic links. This can get tedious in pure shell.
Martin v. Löwis
Because coding anything at all complicated in shell script is an exercise in frustration and weird errors caused by incorrectly-escaped text?
bobince
+3  A: 
import os, time

allfiles = []
now = time.time()

# walk will return triples (current dir, list of subdirs, list of regular files)
# file names are relative to dir at first
for dir, subdirs, files in os.walk("/storage/disk-1/Media/Video/TV"):
    for f in files:
        if not f.endswith(".avi"):
            continue
        # compute full path name
        f = os.path.join(dir, f)
        st = os.stat(f)
        if st.st_mtime < now - 3600*24*7:
            # too old
            continue
        allfiles.append((f, st))

This will return all files that find also returned, as a list of pairs (filename, stat result).

Martin v. Löwis
Awesome! Thanks for the code example, really helpful.
nbolton
The interesting thing about this example is that the time is from when the search started, rather than "now." So if it takes longer than 7 days to search the file system, after 7 days no files will be returned. (Perhaps not an issue for 7 days, but an issue of you want all of the files less than 7 minutes old.)
vy32
@simsong: if search takes a significant time, it is just not possible to get correct results merely by traversing the file system. If you assume that new files are created while the search is still in progress, then new files may also get created in parts of the tree that you have traversed already. The search will correctly return all files that were younger than 7 days when the search started.
Martin v. Löwis
Note, that calling `os.stat()` for each file can be too slow for some applications such as file browser. Unfortunately `opendir()` is not available in Python, but it can be called using `ctypes` module.
Denis Otkidach
@Denis: how would opendir help in finding out the last modification time of a file?
Martin v. Löwis