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

looping over the files in a directory

Can anyone please tell me the actual syntax for saying something like this:

for <file> in <directory>:
<do something with the file>

(?) Muchas gracias.

Peace
Jul 18 '05 #1
8 1471
On Fri, 14 May 2004 15:32:32 +0000, Elaine Jackson wrote:
Can anyone please tell me the actual syntax for saying something like this:

for <file> in <directory>:
<do something with the file>


A simple example:

import os

path = '/home/bob/'

for f in os.listdir(path):
print f

--
Mark J. Nenadov
Python Byte Solutions
http://www.pythonbyte.com/
Jul 18 '05 #2
On Fri, 14 May 2004 15:32:32 +0000, Elaine Jackson wrote:
Can anyone please tell me the actual syntax for saying something like this:

for <file> in <directory>:
<do something with the file>

(?) Muchas gracias.


There's also glob which may be useful depending on what you're doing, e.g.

import glob

for f in glob.glob("*.txt"):
print f

Jeremy

Jul 18 '05 #3
Mark J. Nenadov wrote:
On Fri, 14 May 2004 15:32:32 +0000, Elaine Jackson wrote:
Can anyone please tell me the actual syntax for saying something like this:
for <file> in <directory>:
<do something with the file>

A simple example:
import os
path = '/home/bob/'
for f in os.listdir(path):
print f


A further clue for Elaine is:
listdir returns file "names". Files are things the OS has; names are
how we talk about the files to the OS. If you want to do anything with
the file, you'll have to decide what you want to do, and communicate the
_full_name_ of the file to the OS when you try to do it.

You can do that either by joining the path and file names, or by
changing the "working directory" so that the simple file name works.
Which you do depends on your overall goal.

This is purposely left a bit elliptical to leave you some work in
case this is a homework assignment.

--
-Scott David Daniels
Sc***********@Acm.Org
Jul 18 '05 #4
Scott David Daniels <Sc***********@Acm.Org> wrote in
news:40********@nntp0.pdx.net:
This is purposely left a bit elliptical to leave you some work in
case this is a homework assignment.


Sorry for that, but what does mean 'a bit elliptical' in this sentence?
Is it a kind of circumlocution?

--
Krzysztof Szynter :'''. :. : *
Dygi GG 1027078 :...' ..... : : : ..... . . . . . .....
http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :....
dygimail(at)poczta(dot)fm :...' :.... : ': :.... : : :..' : :....
Jul 18 '05 #5
elliptical <=> some information is left unspecified

"Krzysztof Szynter" <dy********@SPAMpoczta.fm> wrote in message
news:Xn******************************@127.0.0.1...
| Scott David Daniels <Sc***********@Acm.Org> wrote in
| news:40********@nntp0.pdx.net:
|
| > This is purposely left a bit elliptical to leave you some work in
| > case this is a homework assignment.
|
| Sorry for that, but what does mean 'a bit elliptical' in this sentence?
| Is it a kind of circumlocution?
|
| --
| Krzysztof Szynter :'''. :. : *
| Dygi GG 1027078 :...' ..... : : : ..... . . . . . .....
| http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :....
| dygimail(at)poczta(dot)fm :...' :.... : ': :.... : : :..' : :....
Jul 18 '05 #6
Works like a charm. Thanks for the tip.

"Mark J. Nenadov" <ma**@freelance-developer.com> wrote in message
news:pa****************************@freelance-developer.com...
| On Fri, 14 May 2004 15:32:32 +0000, Elaine Jackson wrote:
|
| > Can anyone please tell me the actual syntax for saying something like this:
| >
| > for <file> in <directory>:
| > <do something with the file>
|
| A simple example:
|
| import os
|
| path = '/home/bob/'
|
| for f in os.listdir(path):
| print f
|
| --
| Mark J. Nenadov
| Python Byte Solutions
| http://www.pythonbyte.com/
Jul 18 '05 #7
"Elaine Jackson" <el***************@home.com> wrote in
news:uLcpc.478556$Ig.443767@pd7tw2no:
elliptical <=> some information is left unspecified


Thanks a lot.

--
Krzysztof Szynter :'''. :. : *
Dygi GG 1027078 :...' ..... : : : ..... . . . . . .....
http://newbie.friko.pl : : :.... : : : :.... :: :: :.. : :....
dygimail(at)poczta(dot)fm :...' :.... : ': :.... : : :..' : :....
Jul 18 '05 #8
On Fri, 14 May 2004 15:32:32 GMT, "Elaine Jackson"
<el***************@home.com> wrote:
Can anyone please tell me the actual syntax for saying something like this:

for <file> in <directory>:
<do something with the file>

(?) Muchas gracias.

Peace


Mark Nenadov posted a solution for a single directory. If you want to
do so recursively, try something like this:

import os
for root, d, files in os.walk("."):
for basename in files:
filename = "%s/%s" % (root, basename)
print filename # or open it, or whatever...

--dang
Jul 18 '05 #9

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

Similar topics

1
by: davidz | last post by:
I am attempting to use the foreach loop structure in an SSIS package to loop through however many Excel files are placed in a directory and then perform an import operation into a SQL table on each...
2
by: Mark | last post by:
I am trying to loop through 2 separate directories (using the Dir) at the same time but it seems to get confused and is mixing the files from both directories. I tried to use the My.Computer.Get...
1
by: Alex | last post by:
Hello, I have a stored procedure that processes an individual file from a directory and archives it in a subdirectory.Now, the problem is, when i execute it , it will only process one file. What...
3
by: Kannan | last post by:
HI, I am new to C# and It might be a simple question. I want to loop through filenames and ignore subfolder and its files. I have to display all files names in the screen. Please help me how...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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$) { } ...
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...

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.