From: loial on
In a unix shell script I can do something like this to look in a
directory and get the name of a file or files into a variable :

MYFILE=`ls /home/mydir/JOHN*.xml`


Can I do this in one line in python?

From: Alex Willmer on
On Aug 3, 11:21 am, loial <jldunn2...(a)gmail.com> wrote:
> In a unix shell script I can do something like this to look in a
> directory and get the name of a file or files into a variable :
>
> MYFILE=`ls /home/mydir/JOHN*.xml`
>
> Can I do this in one line in python?

Depends if you count imports.

import glob
my_files = glob.glob('/home/mydir/JOHN*.xml')

Regards, Alex
From: Matteo Landi on
I suggest you to take a look at walk function inside the os module
[1]; IIRC, on the list you would find a discussion on how to create a
wrapper for os.walk with support for filters or wildcards.

Regards.

[1] http://docs.python.org/library/os.html?highlight=os.walk#os.walk

On Tue, Aug 3, 2010 at 12:21 PM, loial <jldunn2000(a)gmail.com> wrote:
> In a unix shell script I can do something like this to look in a
> directory and get the name of a file or files into a variable :
>
> MYFILE=`ls /home/mydir/JOHN*.xml`
>
>
> Can I do this in one line in python?
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>



--
Matteo Landi
http://www.matteolandi.net/
From: loial on
On 3 Aug, 11:31, Alex Willmer <a...(a)moreati.org.uk> wrote:
> On Aug 3, 11:21 am, loial <jldunn2...(a)gmail.com> wrote:
>
> > In a unix shell script I can do something like this to look in a
> > directory and get the name of a file or files into a variable :
>
> > MYFILE=`ls /home/mydir/JOHN*.xml`
>
> > Can I do this in one line in python?
>
> Depends if you count imports.
>
> import glob
> my_files = glob.glob('/home/mydir/JOHN*.xml')
>
> Regards, Alex

Cheers
From: Rory Campbell-Lange on
On 03/08/10, Alex Willmer (alex(a)moreati.org.uk) wrote:
> On Aug 3, 11:21?am, loial <jldunn2...(a)gmail.com> wrote:
> > In a unix shell script I can do something like this to look in a
> > directory and get the name of a file or files into a variable :
> >
> > MYFILE=`ls /home/mydir/JOHN*.xml`
> >
> > Can I do this in one line in python?
>
> import glob
> my_files = glob.glob('/home/mydir/JOHN*.xml')

import os; my_files = [f for f in os.listdir('/home/mydir/') if 'JOHN' in f and 'xml' in f]

But in fact glob uses os.listdir and fnmatch.fnmatch functions
internally, so is definitely the way to go.

http://docs.python.org/library/glob.html

--
Rory Campbell-Lange
rory(a)campbell-lange.net
 |  Next  |  Last
Pages: 1 2
Prev: PIL
Next: I GOT $2500 FROM PAYPAL....