473,388 Members | 1,213 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

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
3 1426
bartonc
6,596 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
136 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
2,851 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

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

Similar topics

5
by: Brian Hlubocky | last post by:
I'm have a fairly simple (in terms of COM) python program that pulls info from an Access database and creates Outlook contacts from that information. It uses wxPython for gui and works without...
6
by: Igorati | last post by:
I need some ideas of how to accomplish this task. I was told that I just need to save the input in a file and make the file searchable, nothing fancy like tying into SQL or Oracle. THis is a basic...
5
by: max(01)* | last post by:
hello. i wrote a very simple tkinter demo program that uses menus, buttons, labels, entries, frames and secondary toplevels. it is a python version of a java program made by a colleague. ...
50
by: diffuser78 | last post by:
I have just started to learn python. Some said that its slow. Can somebody pin point the issue. Thans
17
by: hanumizzle | last post by:
I have used Perl for a long time, but I am something of an experimental person and mean to try something new. Most of my 'work' with Vector Linux entails the use of Perl (a bit of a misnomer as it...
3
by: Dara Durum | last post by:
Hi ! Py2.4, Win32. I need to optimize a program that have a speciality: hash (MD5/SHA1) the file contents (many files). Now I do this in a simple python program, because (what a pity) the...
8
by: Eric_Dexter | last post by:
I was looking for a simple way to load a simple python program from another python program. I tried os.system(cabel) The file name is cabel.py a csound instrument editor.. The error I am...
10
true911m
by: true911m | last post by:
This is a simple walkthrough to get PyInstaller up and running. I decided to give PI a try, because it claims to be more selective about what it bundles into its executable files by default, and...
27
by: Paulo da Silva | last post by:
Hi! I was told in this NG that string is obsolet. I should use str methods. So, how do I join a list of strings delimited by a given char, let's say ','? Old way:
6
by: Terry Carroll | last post by:
I am trying to do something with a very large tarfile from within Python, and am running into memory constraints. The tarfile in question is a 4-gigabyte datafile from freedb.org,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.