• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • 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

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

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