ls files --> list packer

Discussion in 'Python' started by kpp9c, Feb 24, 2006.

  1. kpp9c

    kpp9c Guest

    I would like to use the power of Python to build some list structures
    for me.

    Namely i have organized a bunch of folders that have soundfiles in them
    and would like Python to slurp up all the .aif/.aiff (or .wav whatever)
    files in a given set of directories. My friend hacked up this is perl:

    $files = `ls /snd/Public/*.aiff`;

    @snd_filelist = split('\n',$files);

    $i = 0;
    while ($file = @snd_filelist[$i]) {
    print "file $i = @snd_filelist[$i]\n";
    $i++;
    }


    The only catch with the above code (besides its hideousness hee hee) is
    if you have a directory w/i the structure, but in general it works and
    with this i can just put gobs of files into separate dirs pack them
    into a list and then send them to my script that scrambles them and
    plays them.

    I would like something similar, that works with python that is more
    elegant and maybe even more robust.

    but i am failing miserably and my perl friends mock me.

    cheers,
    kp8
     
    kpp9c, Feb 24, 2006
    #1
    1. Advertising

  2. kpp9c

    kpp9c Guest

    and one example of a slightly fancier version would be a variation that
    looks recursively into subdirectories and makes separate lists for each
    subdirectory encountered.

    so if i had a directory called "~/snd/"

    and in "~/snd/" i had:

    "~/snd/one/"
    "~/snd/two/"
    "~/snd/three/"

    each with soundfiles in it.

    I could get those packed in three separate lists named after the
    directory or some such thing....

    This would be so awesome because my carefully organizing your
    directory, you would be carefully
    organizing your data, change your dir structure or add/delete some
    files and you would get a new structure in your script... prolly would
    work with scripting your iTunes music folder too...

    gosh ... sorry ... just thinking out-loud here and getting kind of
    giddy! reaching for the python book ...
     
    kpp9c, Feb 24, 2006
    #2
    1. Advertising

  3. kpp9c

    I V Guest

    kpp9c wrote:
    > Namely i have organized a bunch of folders that have soundfiles in them
    > and would like Python to slurp up all the .aif/.aiff (or .wav whatever)
    > files in a given set of directories. My friend hacked up this is perl:
    >
    > $files = `ls /snd/Public/*.aiff`;


    You could use posix.popen to duplicate the perl hack:

    files = posix.popen('ls /snd/Public/*.aiff').read().strip()

    > @snd_filelist = split('\n',$files);


    snd_filelist = files.split('\n')

    > I would like something similar, that works with python that is more
    > elegant and maybe even more robust.


    Lucklily, python lets you avoid this kind of horrible hack. Try
    os.listdir:

    snd_filelist = [f for f in os.listdir('/snd/Public/') if
    f.endswith('.aiff')]

    I think it's more elegant, and it's certainly more robust.

    > but i am failing miserably and my perl friends mock me.


    Now you get to mock your perl friends!
     
    I V, Feb 24, 2006
    #3
  4. kpp9c

    kpp9c Guest

    cool i just tried:

    >>> import os
    >>>snd_filelist = [f for f in os.listdir('/Users/foo/snd') if f.endswith('.aif')]



    and it worked! and will take a huge bite out of my big script ... which
    i make by doing an ls
    in the terminal and editing (boo hoo)

    one one lc and one import!

    cool..

    that other sillyness i mentioned is not strickly required ... just
    dreaming but i know involves some kind of os walk type thing prolly ...
    meanwhile this is so exciting!

    Thank you!!!!
     
    kpp9c, Feb 24, 2006
    #4
  5. kpp9c

    kpp9c Guest

    gosh i could even use other string methods like startswith to take all
    the files in a given directory which i have organized with a prefix and
    have them stuffed in different lists ... i think ...

    snd_filelist = [f for f in os.listdir('/Users/foo/snd') if
    f.endswith('.aif') & f.startswith('r')]

    \m/ (>.<) \m/

    yeah!

    runnin' to the interpreta now...
     
    kpp9c, Feb 24, 2006
    #5
  6. I V wrote:

    > snd_filelist = [f for f in os.listdir('/snd/Public/') if
    > f.endswith('.aiff')]


    Or even

    from glob import glob

    snd_filelist = glob('/snd/Public/*.aiff')

    Jeremy

    --
    Jeremy Sanders
    http://www.jeremysanders.net/
     
    Jeremy Sanders, Feb 24, 2006
    #6
  7. kpp9c

    Singletoned Guest

    Try using The Path module:
    http://www.jorendorff.com/articles/python/path/.

    I wrote a little script to traverse a directory structure which you
    could use. (You just pass a function to it and it runs it on each file
    in the directory. You want it to run on each directory instead, so
    I've changed it a little for you).

    import path

    def traverse(directory, function, depth=0, onfiles=True, ondirs=False):
    thedir = path.path(directory)
    if onfiles == True:
    for item in thedir.files():
    function(item, depth)
    if ondirs == True:
    for item in thedir.dirs():
    function(item, depth)
    for item in thedir.dirs():
    traverse(item, function, depth+1, onfiles, ondirs)

    You can use it like so:

    def printaifs(thedir, depth):
    print thedir.name
    for item in thedir.files("*.aif'"):
    print "\t" + item

    traverse(r"/Users/foo/snd", printaifs, onfiles=False, ondirs=True)

    NB: I've quickly adapted this whilst away from an installation of
    Python, so it is untested, but should mostly work, unless there's a
    typo.

    Hope this helps at least a little...

    Ed

    PS The Python Tutor list tends to be a much better place to discuss
    this kind of stuff. If you haven't yet encountered Kent and Alan's
    help, then you have a joyous experience ahead of you.
     
    Singletoned, Feb 24, 2006
    #7
  8. kpp9c

    Magnus Lycka Guest

    kpp9c wrote:
    > that other sillyness i mentioned is not strickly required ... just
    > dreaming but i know involves some kind of os walk type thing prolly ...


    os.walk isn't exactly rocket science... Something similar to this?

    >>> import os
    >>> for dir, dirs, files in os.walk('.'):

    .... txt_files = [x for x in files if x.endswith('.txt')]
    .... if txt_files:
    .... print dir, txt_files
     
    Magnus Lycka, Feb 24, 2006
    #8
  9. kpp9c

    kpp9c Guest

    that is nice.... but the little further wrinkle, which i have no idea
    how to do, would be to have the contents of each directory packed into
    a different list.... since you have no idea before hand how many lists
    you will need (how many subdirs you will enounter) ... well that is
    where the hairy part comes in...

    -kp--
     
    kpp9c, Feb 24, 2006
    #9
  10. kpp9c

    kpp9c Guest

    os.listdir works great ... just one problem, it packs the filenames
    only into a list... i need the full path and seach as i might i se NO
    documentation on python.org for os.listdir()

    how do i either grab the full path or append it later ...
     
    kpp9c, Feb 27, 2006
    #10
  11. kpp9c

    Kent Johnson Guest

    kpp9c wrote:
    > os.listdir works great ... just one problem, it packs the filenames
    > only into a list... i need the full path and seach as i might i se NO
    > documentation on python.org for os.listdir()


    Docs for os.listdir() are here:
    http://docs.python.org/lib/os-file-dir.html

    When all else fails try the index:
    http://docs.python.org/lib/genindex.html
    >
    > how do i either grab the full path or append it later ...


    Use os.path.join():

    base = '/some/useful/path'
    for f in os.listdir(base):
    full_path_to_file = os.path.join(base, f)

    Kent
     
    Kent Johnson, Feb 27, 2006
    #11
  12. kpp9c

    kpp9c Guest

    kpp9c, Feb 27, 2006
    #12
  13. kpp9c

    kpp9c Guest

    nice! two little lines that do a boatload of work! hee hee

    ----
    pth = '/Users/kpp9c/snd/01'
    samples = [os.path.join(pth, f) for f in os.listdir(pth) if
    f.endswith('.aif')]
    ----

    thank you Kent! (and Jeremy and Magnus and Singletoned and I V ... and
    john boy and mary ellen .. )
     
    kpp9c, Feb 27, 2006
    #13
  14. kpp9c wrote:
    > Thank you... i was looking in the wrong place cause all i found was
    > this relatively useless doc:
    > http://docs.python.org/lib/module-os.html
    > which says almost nothing.
    >

    In one of its subsections, cleverly named "Files and Directories",
    I see a nice description of listdir.

    http://docs.python.org/lib/os-file-dir.html

    You also might want to read about os.walk in the same page.
    In the os.path module you can see more path name manipulations.
    If you intend to know a language, you should read its manuals
    fast; what you want is a vague impression where information
    lives and what information is there. Maybe half a year later
    do it again. After that every couple of years often suffices.
    At the very least, go through the full tutorial, read docs on
    the "obvious" modules for everyone and for your particular
    area of endeavor, and then on a snacking basis get yourself
    through the rest unless you decide that you never want to deal
    with, for example, unix-specific services or internet protocols.

    Don't expect to acquire _any_ language with "just in time" (JIT)
    techniques. Perhaps JIT works for magic. When you acquire a
    language with JIT, you miss the subtleties of the language.
    You will be doomed to writing the same kinds of programs in every
    language you touch ("writing Fortran in Algol" is what we used to
    call it). I've worked on code that was Java-in-Python, and it was
    frustratingly hard to understand.

    --Scott David Daniels
     
    Scott David Daniels, Feb 27, 2006
    #14
  15. kpp9c

    Magnus Lycka Guest

    kpp9c wrote:
    > that is nice.... but the little further wrinkle, which i have no idea
    > how to do, would be to have the contents of each directory packed into
    > a different list.... since you have no idea before hand how many lists
    > you will need (how many subdirs you will enounter) ... well that is
    > where the hairy part comes in...


    What's the problem? If you'll get an unknown bundle of objects in a
    program, you just put them in a container. A list or a dict will do
    fine. Have a look at the Python tutorial.

    You get a file list for each directory from os.walk. You either keep
    a list called "directories" and for each turn in the loop, you do
    "directories.append((dir, sound_files))", or you have a dict called
    "directories", and do "directories[dir] = sound_files" in the loop.

    Something like this untested code:

    def isSoundFile(x):
    # home work

    dirs = {}
    root = raw_input('start directory')
    for dir, dummy, files in os.walk(root):
    dirs[dir] = [x for x in files if isSoundFile(x)]

    for dir in sorted(dirs):
    print dir
    for fn in dirs[dir]:
    print "\t", fn
    print
     
    Magnus Lycka, Feb 27, 2006
    #15
    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. David Garamond

    exerb: packer?

    David Garamond, Feb 1, 2004, in forum: Ruby
    Replies:
    7
    Views:
    134
    Jim Moy
    Feb 4, 2004
  2. PAR, PAR::Packer

    , Dec 1, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    245
    John Bokma
    Dec 1, 2006
  3. Yakov

    pp question (PAR::Packer)

    Yakov, Apr 8, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    128
    Bart Lateur
    Apr 10, 2007
  4. Replies:
    0
    Views:
    251
  5. Oliver Soeder
    Replies:
    1
    Views:
    289
Loading...

Share This Page