Converting Perl to Python :

Posted on 2011-10-06
Last Modified: 2012-05-12
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";}
Question by:Justin_Edmands
    1 Comment
    LVL 16

    Accepted 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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    Title # Comments Views Activity
    MaxSpan challenge 9 49
    Python 2.7 - French characters 6 27
    mapShare challenge 13 40
    Copy Files - Python 7 28
    The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
    The purpose of this article is to demonstrate how we can use conditional statements using Python.
    Learn the basics of lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
    Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

    758 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now