[Tutor] Regular expressions...

Kent Johnson kent37 at tds.net
Fri May 2 18:23:46 CEST 2008


On Fri, May 2, 2008 at 12:08 PM, Spencer Parker <inthefridge at gmail.com> wrote:
> I need to use a regular expression to get a couple of items for my python
> script.  So far the script is running an 'ls' command to get a few items
> that I need
>
> I run an 'ls -sk /xen/domains2/machinename/disk.img'
>
> Output
>
> 2454112 /xen/domains2/machinename/disk.img

Look at os.path.getsize(). You don't need to use ls to get this information.

> What I need to get is just the size numbers using regular expressions.

You could just use str.split() but getsize() is better.

> I also need to get the 'machinename'
> portion of the path as well to stick into the database.

Don't you already know this? Where does the ls command come from?
Anyway look at os.path.split() and os.path.dirname().

Kent


More information about the Tutor mailing list