Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

file traversal

somasundar11somasundar11 Posts: 3Member
i need to parse through a list of files in a folder and separate only those files with a particular extension such as doc or htm files. anyone has any idea how to write a python script for this?

Comments

  • DrostDrost Posts: 24Member
    : i need to parse through a list of files in a folder and separate only those files with a particular extension such as doc or htm files. anyone has any idea how to write a python script for this?
    :

    [code]
    import glob
    location = "./" #path where you search
    particularext = glob.glob(location + '*.doc')
    particularext.extend(glob.glob(location + '*.htm'))
    [/code]

    Here you'll get a list of those filenames.

    Drost

  • somasundar11somasundar11 Posts: 3Member

    thanks drost
    glob seems to specific to Unix , i tried this in my NT m/c it doesnt give any results, although no errors are thrown, can u pl try this?:

    i tried this :
    import glob
    location="C: sm_imagesTSM_BA_Client"
    particularext=glob.glob(location+'*.txt')
    particularext.extend(glob.glob(location+'*.html'))
    i doesnt give any results for any folder or any existing file extension.

    : i need to parse through a list of files in a folder and separate only those files with a particular extension such as doc or htm files. anyone has any idea how to write a python script for this?
    : :
    :
    : [code]
    : import glob
    : location = "./" #path where you search
    : particularext = glob.glob(location + '*.doc')
    : particularext.extend(glob.glob(location + '*.htm'))
    : [/code]
    :
    : Here you'll get a list of those filenames.
    :
    : Drost
    :
    :

  • DrostDrost Posts: 24Member
    [b][red]This message was edited by Drost at 2004-9-10 3:18:57[/red][/b][hr]
    :
    : thanks drost
    : glob seems to specific to Unix , i tried this in my NT m/c it doesnt give any results, although no errors are thrown, can u pl try this?:
    :
    : i tried this :
    : import glob
    : location="C: sm_imagesTSM_BA_Client"
    : particularext=glob.glob(location+'*.txt')
    : particularext.extend(glob.glob(location+'*.html'))
    : i doesnt give any results for any folder or any existing file extension.
    :

    Glob works on windows but be aware that to use pathstrings on windows you need to use either raw strings or double your slashes so that you don't trigger the escape character meaning of single slashes ( means tab, etc.)...
    So your location should be like

    [code]
    location = r"C: sm_imagesTSM_BA_Client"
    -----------!
    or

    location = "C:\tsm_images\TSM_BA_Client"
    --------------!-----------!
    [/code]

    But then the rest needs the following correction (my fault). Strings don't like odd number of slashes at the end...

    [code]
    particularext=glob.glob(location + r'*.txt')
    particularext.extend(glob.glob(location + r'*.html')
    [/code]

    Drost


  • somasundar11somasundar11 Posts: 3Member
    hi drost,
    thanks once again for the suggestion
    i once again tried your suggestion like this :looking for two text files created in C:\Documents and Settings

    import glob
    location= "C:\Documents and Settings"
    particularext=glob.glob(location+r'*.txt')
    particularext.extend(glob.glob(location+r'*.html'))

    the run doesnt give me any results ? does it have any other missing element?
    thanks somuch for your patience ,
    som





    [b][red]This message was edited by Drost at 2004-9-10 3:18:57[/red][/b][hr]
    : :
    : : thanks drost
    : : glob seems to specific to Unix , i tried this in my NT m/c it doesnt give any results, although no errors are thrown, can u pl try this?:
    : :
    : : i tried this :
    : : import glob
    : : location="C: sm_imagesTSM_BA_Client"
    : : particularext=glob.glob(location+'*.txt')
    : : particularext.extend(glob.glob(location+'*.html'))
    : : i doesnt give any results for any folder or any existing file extension.
    : :
    :
    : Glob works on windows but be aware that to use pathstrings on windows you need to use either raw strings or double your slashes so that you don't trigger the escape character meaning of single slashes ( means tab, etc.)...
    : So your location should be like
    :
    : [code]
    : location = r"C: sm_imagesTSM_BA_Client"
    : -----------!
    : or
    :
    : location = "C:\tsm_images\TSM_BA_Client"
    : --------------!-----------!
    : [/code]
    :
    : But then the rest needs the following correction (my fault). Strings don't like odd number of slashes at the end...
    :
    : [code]
    : particularext=glob.glob(location + r'*.txt')
    : particularext.extend(glob.glob(location + r'*.html')
    : [/code]
    :
    : Drost
    :
    :
    :

  • DrostDrost Posts: 24Member
    [b][red]This message was edited by Drost at 2004-9-22 4:7:51[/red][/b][hr]
    : hi drost,
    : thanks once again for the suggestion
    : i once again tried your suggestion like this :looking for two text files created in C:\Documents and Settings
    :
    : import glob
    : location= "C:\Documents and Settings"
    : particularext=glob.glob(location+r'*.txt')
    : particularext.extend(glob.glob(location+r'*.html'))
    :
    : the run doesnt give me any results ? does it have any other missing element?
    : thanks somuch for your patience ,
    : som

    Sorry for the late answer.
    Funnily that code works for me...

    [code]
    import glob
    location= "C:\Documents and Settings"
    particularext=glob.glob(location+r'*.txt')
    particularext.extend(glob.glob(location+r'*.html'))
    raw_input(particularext) #this waits for an Enter after printing the result
    [/code]

    Do such files really exist in that directory?
    You probably have had reading rights too if you were able to create those files in the first place.

    So you say it produced only an empty list in the end?

    Drost

Sign In or Register to comment.