473,394 Members | 1,812 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,394 software developers and data experts.

get directory and file names

I am working with Python 2.5 on Windows XP (SP2).

How can I traverse a folder, loop through a list of files and get
their file name and extension in an elegant, pythonic way?

Thank you.

Jul 31 '07 #1
3 19860
On Tue, 31 Jul 2007 07:23:06 -0700, Alchemist wrote:
I am working with Python 2.5 on Windows XP (SP2).

How can I traverse a folder, loop through a list of files and get
their file name and extension in an elegant, pythonic way?
Take a look at the `os` and the `os.path` modules. In particular
`os.listdir()`, `os.path.isfile()` and `os.path.splitext()`.

Ciao,
Marc 'BlackJack' Rintsch
Jul 31 '07 #2
Alchemist a écrit :
I am working with Python 2.5 on Windows XP (SP2).

How can I traverse a folder, loop through a list of files and get
their file name and extension in an elegant, pythonic way?
http://docs.python.org/lib/module-os.path.html
http://docs.python.org/lib/os-file-dir.html
(=os.walk)

Jul 31 '07 #3
Alchemist wrote:
I am working with Python 2.5 on Windows XP (SP2).

How can I traverse a folder, loop through a list of files and get
their file name and extension in an elegant, pythonic way?

Thank you.
try this:

for root, dirs, files in os.walk('.'):
for f in files:
print os.path.splitext(os.path.join(root,f))

Jul 31 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: LELAND PRINCE | last post by:
Thanks, this group has been a big help. I need to create a backup copy of a working file that changes everyday, but I need to maintain a copy from each day. Would like to create: back1.txt...
2
by: Maciej | last post by:
Hi, Is there any simple way to get file names from a specified directory? I have a program which converts files one by one and I need to convert thousands of them. -- Maciej Paprota
6
by: Mark | last post by:
I want the VB.Net program to read and file names from a directory. I thought the best thing to do was to place these file names into a "temporary" MS Access database for sorting, etc. So I'm...
2
by: Jacob Lyles | last post by:
Hello, I'm using the following function to display a list of files in a directory in a HTML form that allows the user to delete files. Look at the echo line. The links work fine, taking a user...
3
by: cjb | last post by:
Is there a way to get Directory.GetFiles to return multi-language file names? I haven't found any overloads that allow any such parameter. The way I am using it now is: foreach (string d in...
2
by: xeshu | last post by:
HelowWw fellow programmers :) I have a web application that opens a pdf file. Very simple indeed. However the file name is not known. The file has to be found by first searching the list of all...
5
by: johnwayne83 | last post by:
This is my first semester in c++ and i took java last semester. I need some help and i really cant find any information that has helped me or to point me in the right direction. I am using...
2
by: SantaClaus | last post by:
Hi all, i need to recursively get all the file names in a directory tree in C++. Im using win32 API to get the directory names and recursively calling the same function. Could anyone please let...
2
by: gunna | last post by:
Hello I am making a website and I want to use ASP file names instead of HTML file names because I think it will block subpages from opening individually without their parent pages. I have IIS...
1
by: mujunshan | last post by:
Yesterday, I installed PythonCE on my cellphone whose OS is Windows Mobile 5.I wanted to use numpy as calculation tool.But after I copy numpy module in my desktop computer into my phone,I find many...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.