looping over the files in a directory

E

Elaine Jackson

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
 
M

Mark J. Nenadov

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
 
J

Jeremy Sanders

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
 
S

Scott David Daniels

Mark said:
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.
 
K

Krzysztof Szynter

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?
 
E

Elaine Jackson

elliptical <=> some information is left unspecified

| |
| > 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 :...' :.... : ': :.... : : :..' : :....
 
E

Elaine Jackson

Works like a charm. Thanks for the tip.

| 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/
 
D

Daniel 'Dang' Griffith

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top