On 08/03/10 06:21, quoth loial:
> 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?
> 

Sorry, but I just can't help myself.

Yeah, it's one shell line, but why the extra process, setup of pipes,
teardown, and all the rest when you can just say

MYFILE=/home/mydir/JOHN*.xml

After all, your way just starts a subshell which runs ls in a grandchild
process and creates a pipe to read back what the subshell writes. All the
subhell does is to run ls on what the shell globs. And without any options to
the ls command, you're just as well off by using echo instead of ls.

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

Since echo is probably a builtin, you'd be creating a child but no grandchild.

Other than that, the use of glob in python answers your question well (unless
someone wants to write up how to do it in python by use of the subprocess
module along with the glob module...)

-- 
Time flies like the wind. Fruit flies like a banana. Stranger things have  .0.
happened but none stranger than this. Does your driver's license say Organ ..0
Donor?Black holes are where God divided by zero. Listen to me! We are all- 000
individuals! What if this weren't a hypothetical question?
steveo at syslang.net

Attachment: signature.asc
Description: OpenPGP digital signature

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to