467,915 Members | 1,314 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,915 developers. It's quick & easy.

Very simple Python program help...

I am extremely new to python and I need to write a program that simply outputs all the filenames within a specific directory to a text file. Thanks!
May 7 '07 #1
  • viewed: 1304
Share:
3 Replies
bartonc
Expert 4TB
I am extremely new to python and I need to write a program that simply outputs all the filenames within a specific directory to a text file. Thanks!
The glob module may be what you are looking for:
6.24 glob -- Unix style pathname pattern expansion

The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell. No tilde expansion is done, but *, ?, and character ranges expressed with [] will be correctly matched. This is done by using the os.listdir() and fnmatch.fnmatch() functions in concert, and not by actually invoking a subshell. (For tilde and shell variable expansion, use os.path.expanduser() and os.path.expandvars().)

For example, consider a directory containing only the following files: 1.gif, 2.txt, and card.gif. glob() will produce the following results. Notice how any leading components of the path are preserved.

>>> import glob
>>> glob.glob('./[0-9].*')
['./1.gif', './2.txt']
>>> glob.glob('*.gif')
['1.gif', 'card.gif']
>>> glob.glob('?.gif')
['1.gif']
May 7 '07 #2
dshimer
Expert 100+
Depending on what you want to do, once you get all the filenames into a list using glob you may also find some help in parsing filenames into individual parts (drive, path, base name, extension) using the os.path module. I find these two modules to be a huge help in working with file names.
May 7 '07 #3
bvdet
Expert Mod 2GB
Depending on what you want to do, once you get all the filenames into a list using glob you may also find some help in parsing filenames into individual parts (drive, path, base name, extension) using the os.path module. I find these two modules to be a huge help in working with file names.
There are a several threads on the Python forum on compiling a list of file names from a specific directory. In short, the file list can be created like this:
Expand|Select|Wrap|Line Numbers
  1. >>> dn = 'directory_path'
  2. >>> fileStr = '\n'.join([f for f in os.listdir(dn) if os.path.isfile(os.path.join(dn, f))])
Directory listings are skipped in the above list comprehension. To write the resulting string to a disk file:
Expand|Select|Wrap|Line Numbers
  1. f = open('file_name', 'w')
  2. f.write(fileStr)
  3. f.close()
May 7 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

6 posts views Thread by Igorati | last post: by
5 posts views Thread by max(01)* | last post: by
50 posts views Thread by diffuser78 | last post: by
17 posts views Thread by hanumizzle | last post: by
3 posts views Thread by Dara Durum | last post: by
27 posts views Thread by Paulo da Silva | last post: by
6 posts views Thread by Terry Carroll | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.