newbie wants to compile python list of filenames in selected directories

Discussion in 'Python' started by anthonyberet, Feb 6, 2005.

  1. anthonyberet

    anthonyberet Guest

    Hi, I am new at Python, and very rusty at the one language I was good
    at, which was BASIC.

    I want to write a script to compare filenames in chosen directories, on
    windows machines. Ideally it would compose a list of strings of all the
    filenames in the directories, and those directories would be chosable by
    the user of the script.

    I am quite happy to do my own legwork on this , I realise it is simple
    stuff, but can anyone point me in the right direction to start?

    Thanks
     
    anthonyberet, Feb 6, 2005
    #1
    1. Advertising

  2. anthonyberet

    Greg Krohn Guest

    Re: newbie wants to compile python list of filenames in selecteddirectories

    anthonyberet wrote:
    > Hi, I am new at Python, and very rusty at the one language I was good
    > at, which was BASIC.
    >
    > I want to write a script to compare filenames in chosen directories, on
    > windows machines. Ideally it would compose a list of strings of all the
    > filenames in the directories, and those directories would be chosable by
    > the user of the script.
    >
    > I am quite happy to do my own legwork on this , I realise it is simple
    > stuff, but can anyone point me in the right direction to start?
    >
    > Thanks


    os.listdir does almost exactly what you are asking for. For a little
    more flexibility, you can use os.path.walk, although it's not quite as
    newb friendly.

    -greg
     
    Greg Krohn, Feb 6, 2005
    #2
    1. Advertising

  3. anthonyberet

    John J. Lee Guest

    Greg Krohn <> writes:
    > anthonyberet wrote:

    [...]
    > > I want to write a script to compare filenames in chosen directories,
    > > on windows machines. Ideally it would compose a list of strings of
    > > all the filenames in the directories, and those directories would be
    > > chosable by the user of the script.

    [...]
    >
    > os.listdir does almost exactly what you are asking for. For a little
    > more flexibility, you can use os.path.walk, although it's not quite as
    > newb friendly.
    >
    > -greg


    os.walk is perhaps newbie-friendlier than os.path.walk.

    Or perhaps not: not having read the tutorial &c. for some time, I
    don't know how good they are on generators...


    John
     
    John J. Lee, Feb 6, 2005
    #3
  4. anthonyberet

    M.E.Farmer Guest

    anthonyberet wrote:
    > Hi, I am new at Python, and very rusty at the one language I was good


    > at, which was BASIC.
    >
    > I want to write a script to compare filenames in chosen directories,

    on
    > windows machines. Ideally it would compose a list of strings of all

    the
    > filenames in the directories, and those directories would be chosable

    by
    > the user of the script.
    >
    > I am quite happy to do my own legwork on this , I realise it is

    simple
    > stuff, but can anyone point me in the right direction to start?
    >
    > Thanks


    Cool! I like your attitude ;)
    Others have given you a good start with os. In case you don't know os
    has a great many path manipulation methods. Always try to use them so
    you can insulate yourself from cross platform path nightmares.
    A few of the highlights are:

    ### split a path
    py> parts = os.path.split(r'c:\windows\media\ding.wav')
    py> print parts
    ('c:\\windows\\media', 'ding.wav')

    ### join a path and part and do it right on any platform
    py> path = os.path.join('c:\\windows\\media', 'ding.wav')
    'c:\\windows\\media\\ding.wav'

    ### get basename of the file
    py> base = os.path.basename('c:\\windows\\media\\ding.wav')
    py> print base
    'ding.wav'

    Plus many more, be sure to study the os module if you are doing any
    path manipulations.

    Ok and now for something sorta different( alright, I was really bored )

    .. def glob_dir(dir):
    .. """Return a list of *.py* (.py, .pyc, .pyo, .pyw)
    .. files from a given directory.
    .. """
    .. import glob, os
    .. # Get a list of files that match *.py*
    .. GLOB_PATTERN = os.path.join(dir, "*.[p][y]*")
    .. pathlist = glob.glob(GLOB_PATTERN)
    .. return pathlist
    ..
    .. def list_dir(dir):
    .. ''' Another way to filter a dir '''
    .. import os
    .. pathlist = os.listdir(dir)
    .. # Now filter out all but py and pyw
    .. filterlist = [x for x in pathlist
    .. if x.endswith('.py')
    .. or x.endswith('.pyw')]
    .. return filterlist
    hth,
    M.E.Farmer
     
    M.E.Farmer, Feb 7, 2005
    #4
  5. Hi Anthony

    Here is some stuff to get you started (this is from memory, I'm not
    checking it, but should be mostly helpful):

    ***
    import os

    os.chdir('C:\My Documents') # Can use this to jump around to different
    folders
    fileNames = os.listdir('.') # Checks the now current folder
    namesToMatch = ['readme.txt','readme.doc'] # Buncha names to find

    for item in fileNames: # check every filename
    if item in namesToMatch: # is this item in the required list?
    print 'Match found: '+item # if you found one, say so.
    ***

    Hope this helps.
    Caleb




    On Sun, 06 Feb 2005 18:29:34 +0000, anthonyberet <> wrote:

    > Hi, I am new at Python, and very rusty at the one language I was good
    > at, which was BASIC.
    >
    > I want to write a script to compare filenames in chosen directories, on
    > windows machines. Ideally it would compose a list of strings of all the
    > filenames in the directories, and those directories would be chosable by
    > the user of the script.
    >
    > I am quite happy to do my own legwork on this , I realise it is simple
    > stuff, but can anyone point me in the right direction to start?
    >
    > Thanks
     
    Caleb Hattingh, Feb 7, 2005
    #5
  6. anthonyberet

    anthonyberet Guest

    Re: newbie wants to compile python list of filenames in selecteddirectories

    M.E.Farmer wrote:
    > anthonyberet wrote:
    >
    >>Hi, I am new at Python, and very rusty at the one language I was good

    >
    >
    >>at, which was BASIC.
    >>
    >>I want to write a script to compare filenames in chosen directories,

    >
    > on
    >
    >>windows machines. Ideally it would compose a list of strings of all

    >
    > the
    >
    >>filenames in the directories, and those directories would be chosable

    >
    > by
    >
    >>the user of the script.
    >>
    >>I am quite happy to do my own legwork on this , I realise it is

    >
    > simple
    >
    >>stuff, but can anyone point me in the right direction to start?
    >>
    >>Thanks

    >
    >
    > Cool! I like your attitude ;)
    > Others have given you a good start with os. In case you don't know os
    > has a great many path manipulation methods. Always try to use them so
    > you can insulate yourself from cross platform path nightmares.
    > A few of the highlights are:
    >
    > ### split a path
    > py> parts = os.path.split(r'c:\windows\media\ding.wav')
    > py> print parts
    > ('c:\\windows\\media', 'ding.wav')
    >
    > ### join a path and part and do it right on any platform
    > py> path = os.path.join('c:\\windows\\media', 'ding.wav')
    > 'c:\\windows\\media\\ding.wav'
    >
    > ### get basename of the file
    > py> base = os.path.basename('c:\\windows\\media\\ding.wav')
    > py> print base
    > 'ding.wav'
    >
    > Plus many more, be sure to study the os module if you are doing any
    > path manipulations.
    >
    > Ok and now for something sorta different( alright, I was really bored )
    >
    > . def glob_dir(dir):
    > . """Return a list of *.py* (.py, .pyc, .pyo, .pyw)
    > . files from a given directory.
    > . """
    > . import glob, os
    > . # Get a list of files that match *.py*
    > . GLOB_PATTERN = os.path.join(dir, "*.[p][y]*")
    > . pathlist = glob.glob(GLOB_PATTERN)
    > . return pathlist
    > .
    > . def list_dir(dir):
    > . ''' Another way to filter a dir '''
    > . import os
    > . pathlist = os.listdir(dir)
    > . # Now filter out all but py and pyw
    > . filterlist = [x for x in pathlist
    > . if x.endswith('.py')
    > . or x.endswith('.pyw')]
    > . return filterlist
    > hth,


    Thanks to everyone who responded - I feel on my way now, just being able
    to tap 'os python' and 'lisdir python' into Google has netted me plenty
    of reading material - but I would have taken a long time to find that
    out without your tips!
    I will surely come back, but not until I get I get stuck :)
     
    anthonyberet, Feb 8, 2005
    #6
    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. B.J.
    Replies:
    4
    Views:
    772
    Toby Inkster
    Apr 23, 2005
  2. Mike van der Hulst
    Replies:
    3
    Views:
    354
    Branimir Maksimovic
    Oct 21, 2005
  3. raphfrk

    Macro to indicate directories in filenames

    raphfrk, Mar 28, 2008, in forum: C Programming
    Replies:
    8
    Views:
    311
    Antoninus Twink
    Mar 29, 2008
  4. Gerard A.W. Vreeswijk
    Replies:
    0
    Views:
    98
    Gerard A.W. Vreeswijk
    Aug 19, 2003
  5. Deepu
    Replies:
    10
    Views:
    178
    Xicheng Jia
    Mar 16, 2007
Loading...

Share This Page