Get name of file from directory into variable

Discussion in 'Python' started by loial, Aug 3, 2010.

  1. loial

    loial Guest

    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?
     
    loial, Aug 3, 2010
    #1
    1. Advertising

  2. loial

    Alex Willmer Guest

    On Aug 3, 11:21 am, loial <> 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
     
    Alex Willmer, Aug 3, 2010
    #2
    1. Advertising

  3. loial

    Matteo Landi Guest

    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 <> 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/
     
    Matteo Landi, Aug 3, 2010
    #3
  4. loial

    loial Guest

    On 3 Aug, 11:31, Alex Willmer <> wrote:
    > On Aug 3, 11:21 am, loial <> 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
     
    loial, Aug 3, 2010
    #4
  5. On 03/08/10, Alex Willmer () wrote:
    > On Aug 3, 11:21?am, loial <> 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 Campbell-Lange, Aug 3, 2010
    #5
  6. loial

    Matteo Landi Guest

    Ops I miss the "one line" request, so my previous answer was definitely OT.

    glob seems to be your solution.

    On Tue, Aug 3, 2010 at 12:44 PM, Rory Campbell-Lange
    <> wrote:
    > On 03/08/10, Alex Willmer () wrote:
    >> On Aug 3, 11:21?am, loial <> 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
    >
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >




    --
    Matteo Landi
    http://www.matteolandi.net/
     
    Matteo Landi, Aug 3, 2010
    #6
  7. 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


    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v2.0.10 (GNU/Linux)
    Comment: Using GnuPG with Fedora - http://enigmail.mozdev.org/

    iEYEARECAAYFAkxZiswACgkQRIVy4fC+NyTVQQCeIPJaL+YURb7Zcq14hyULrH5E
    EHwAoI0vEb6U6k6My7KgRgVKLzKDBZ+g
    =zhLM
    -----END PGP SIGNATURE-----
     
    Steven W. Orr, Aug 4, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ding feng
    Replies:
    2
    Views:
    2,821
    ding feng
    Jun 25, 2003
  2. Bobby Chamness
    Replies:
    2
    Views:
    2,400
    Joe Smith
    Apr 22, 2007
  3. mfglinux
    Replies:
    11
    Views:
    705
    Roberto Bonvallet
    Sep 12, 2007
  4. Mike Schilling
    Replies:
    2
    Views:
    568
    Arne Vajhøj
    Mar 5, 2010
  5. David Filmer
    Replies:
    19
    Views:
    246
    Kevin Collins
    May 21, 2004
Loading...

Share This Page