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!
3 1426
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']
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.
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: - >>> dn = 'directory_path'
-
>>> 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: - f = open('file_name', 'w')
-
f.write(fileStr)
-
f.close()
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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.
...
|
by: diffuser78 |
last post by:
I have just started to learn python. Some said that its slow. Can
somebody pin point the issue.
Thans
|
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...
|
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...
|
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...
|
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...
|
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:
|
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,...
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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,...
|
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...
| |