• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 448
  • Last Modified:

Converting Perl to Python :

Perl supports a variety of modifiers to opening a file... This specific example uses UNIX find() with the '-newer' option

I need to efficiently convert this to Python for a backup script. I'm thinking I would use subprocess.Popen(cmd, stdout), passing the find command and indexing into stdout to find all files newer than the ARCHIVE file (besides the tarball)

open(CHANGES,"find $dir -newer $dir/ARCHIVED |");
          # There should be only one file newer than ARCHIVE, the *.tgz file
          if ($#changes <= 1) {
            if ($DEBUG) {print "no changes since archive\ndeleting this dir\n";}
1 Solution
Attached script should roughly be the equivalent.
You could write it a little more compact, but then it might not be that easy to understand
!/usr/bin/env python
import subprocess

dirname = '.'
cmd = [ 'find', dirname, '-newer',  '%s/ARCHIVED' % dirname ]
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE)
changes = proc.stdout.readlines()
if len(changes) <= 1:
    print "no changes sice"

Open in new window


Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now