Python

Moderators: None (Apply to moderate this forum)
Number of threads: 474
Number of posts: 1166

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
file traversal Posted by somasundar11 on 7 Sept 2004 at 7:26 AM
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?
Report
Re: file traversal Posted by Drost on 8 Sept 2004 at 3:16 AM
: 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?
:

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


Here you'll get a list of those filenames.

Drost

Report
Re: file traversal Posted by somasundar11 on 8 Sept 2004 at 4:27 AM

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:\tsm_images\TSM_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?
: :
:
:
: import glob
: location = "./" #path where you search
: particularext = glob.glob(location + '*.doc')
: particularext.extend(glob.glob(location + '*.htm'))
: 

:
: Here you'll get a list of those filenames.
:
: Drost
:
:

Report
Re: file traversal Posted by Drost on 10 Sept 2004 at 3:16 AM
This message was edited by Drost at 2004-9-10 3:18:57

:
: 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:\tsm_images\TSM_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 (\t means tab, etc.)...
So your location should be like

location = r"C:\tsm_images\TSM_BA_Client"
-----------!
or

location = "C:\\tsm_images\\TSM_BA_Client"
--------------!-----------!


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

particularext=glob.glob(location + r'\*.txt')
particularext.extend(glob.glob(location + r'\*.html')


Drost


Report
Re: file traversal Posted by somasundar11 on 12 Sept 2004 at 2:07 PM
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





This message was edited by Drost at 2004-9-10 3:18:57

: :
: : 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:\tsm_images\TSM_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 (\t means tab, etc.)...
: So your location should be like
:
:
: location = r"C:\tsm_images\TSM_BA_Client"
: -----------!
: or
: 
: location = "C:\\tsm_images\\TSM_BA_Client"
: --------------!-----------!
: 

:
: But then the rest needs the following correction (my fault). Strings don't like odd number of slashes at the end...
:
:
: particularext=glob.glob(location + r'\*.txt')
: particularext.extend(glob.glob(location + r'\*.html')
: 

:
: Drost
:
:
:

Report
Re: file traversal Posted by Drost on 22 Sept 2004 at 12:45 AM
This message was edited by Drost at 2004-9-22 4:7:51

: 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...

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


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




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.