running actual unix commands

Van Gale news at exultants.org
Wed May 28 04:26:05 EDT 2003


Andrew Thomson wrote:
> just trying more approaches now...
> 
> files = os.listdir(dir)
> print files
> 
> for file in files:
>    fullfile = os.path.join(dir,file)
>    print fullfile
>    print os.path.getatime(fullfile)

I think this is a much better approach than stringing together shell 
commands.

If you are running python 2.2 you should take a look at the excellent 
path module by Jason Orendorff

<http://www.jorendorff.com/articles/python/path/>

It'll let you do something like (warning: untested):

dir = path('/path')
for f in dir.files('*.ajt')
     print f
     print f.atime

Since the find command from your shell example is recursive you can 
change the for loop to:

for f in dir.walkfiles('*.ajt')

for similar behaviour.

>>not sure if this is the best way, but i'm attempting the following:
>>
>>cmd = "find " + dir + " " + "-type f -name \"*.ajt\" -amin +" + time + "
>>" + "| wc -l | awk \'{print $1}\'"
>>files = os.system(cmd)
>>print files
>>
>>however as some might predict, the output of this program isn't what I
>>expect..
>>
>>./test2.py
>>3
>>0

The first thing to try, of course, is run the same command from shell 
command line until it works the way you want... then paste into Python 
script :)

I can't test the script above because there seems to be some missing 
data between + "" + "| wc -l ...

Van





More information about the Python-list mailing list