By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,932 Members | 1,908 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,932 IT Pros & Developers. It's quick & easy.

looping over the files in a directory

P: n/a
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
Share this Question
Share on Google+
8 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.