473,386 Members | 1,830 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,386 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 19859
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...

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.