473,398 Members | 2,113 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,398 software developers and data experts.

Walk thru each subdirectory from a top directory


i am trying to use python to walk thru each subdirectory from a top
directory. Here is my script:

savedPagesDirectory = "/home/meryl/saved_pages/data"

dir=open(savedPagesDirectory, 'r')

for file in dir:
if (isdir(file)):
# get the full path of the file
fileName = savedPagesDirectory + file + 'index.html'
print fileName

$ ./scripts/regressionTest.py
Traceback (most recent call last):
File "./scripts/regressionTest.py", line 12, in ?
dir=open(savedPagesDirectory, 'r')
IOError: [Errno 21] Is a directory

But I get the above error:

Can you please tell me what did I do wrong?

Thank you.

Feb 26 '07 #1
9 2848
si***************@gmail.com schrieb:
i am trying to use python to walk thru each subdirectory from a top
directory. Here is my script:

savedPagesDirectory = "/home/meryl/saved_pages/data"

dir=open(savedPagesDirectory, 'r')

for file in dir:
if (isdir(file)):
# get the full path of the file
fileName = savedPagesDirectory + file + 'index.html'
print fileName

$ ./scripts/regressionTest.py
Traceback (most recent call last):
File "./scripts/regressionTest.py", line 12, in ?
dir=open(savedPagesDirectory, 'r')
IOError: [Errno 21] Is a directory

But I get the above error:

Can you please tell me what did I do wrong?
You can't open a directory.

Use the function os.walk to do what you want.

Diez
Feb 26 '07 #2
On Feb 26, 9:28 pm, silverburgh.me...@gmail.com wrote:
i am trying to use python to walk thru each subdirectory from a top
directory. Here is my script:

savedPagesDirectory = "/home/meryl/saved_pages/data"

dir=open(savedPagesDirectory, 'r')

for file in dir:
if (isdir(file)):
# get the full path of the file
fileName = savedPagesDirectory + file + 'index.html'
print fileName

$ ./scripts/regressionTest.py
Traceback (most recent call last):
File "./scripts/regressionTest.py", line 12, in ?
dir=open(savedPagesDirectory, 'r')
IOError: [Errno 21] Is a directory

But I get the above error:

Can you please tell me what did I do wrong?

Thank you.
>From Alan Gaulds Tut.
>>for t in os.walk('Root'):
.... print t
....
('Root', ['D1', 'D2', 'D3'], ['FA.txt', 'FB.txt'])
('Root/D1', ['D1-1'], ['FC.txt'])
('Root/D1/D1-1', [], ['FF.txt'])
('Root/D2', [], ['FD.txt'])
('Root/D3', ['D3-1'], ['FE.txt'])
('Root/D3/D3-1', [], ['target.txt'])
>>>

This bit below is from one of my first programs as I'm currently
learning. It is designed to go form the root down and return the full
paths of everything it finds into a list. (I then check the reults for
files paths that exceed a certain length - but you don't need to know
that.)
def findallfiles(self, base):
self.results = []
for root,dirs,files in os.walk(base):
os.chdir(root)
self.scan = glob.glob("*")
for r in self.scan:
if root[-1] == "\\":
self.results.append(root + r)
else:
self.results.append(root + "\\" + r)
return self.results
Feb 26 '07 #3
Whoops, the first bit of my reply ended up in the quoted text. See
above.

Adam

Feb 26 '07 #4
Adam wrote:
On Feb 26, 9:28 pm, silverburgh.me...@gmail.com wrote:
>i am trying to use python to walk thru each subdirectory from a top
directory. Here is my script: ....

This bit below is from one of my first programs as I'm currently
learning. It is designed to go form the root down and return the full
paths of everything it finds into a list. (I then check the reults for
files paths that exceed a certain length - but you don't need to know
that.)
def findallfiles(self, base):
self.results = []
for root,dirs,files in os.walk(base):
os.chdir(root)
^^^ Mistake here, don't change directories during os.walk ^^^
self.scan = glob.glob("*")
for r in self.scan:
if root[-1] == "\\":
self.results.append(root + r)
else:
self.results.append(root + "\\" + r)
return self.results
def produce_all_files(base):
for root, dirs, files in os.walk(base):
for r in files:
yield os.path.join(root, r)
### possibly also (but I'd only go for files)
#for r in dirs:
# yield os.path.join(root, r)

def findallfiles(base):
return list(produce_all_files(base))

--
--Scott David Daniels
sc***********@acm.org
Feb 28 '07 #5
On Tue, 27 Feb 2007 20:31:43 -0800, Scott David Daniels wrote:
> def findallfiles(self, base):
self.results = []
for root,dirs,files in os.walk(base):
os.chdir(root)
^^^ Mistake here, don't change directories during os.walk ^^^
I came across this problem some time ago. I had to walk a directory tree,
calling an external program on each file. Unfortunately, that external
program wrote directly to the current working directory, which caused all
sorts of havoc. This is how I dealt with it:
def unbin(where):
"""Walk through a directory tree, calling macunpack to extract the
contents of MacBinary files.
"""
def _unbin(data, dirname, files):
for oldname in files:
fullname = os.path.normpath(os.path.join(dirname, oldname))
if os.path.isfile(fullname):
# Dammit, macunpack writes directly to the current
# working directory. Changing the cwd breaks the file
# tree walker, so we have to remember the current
# directory, change it to where we want to be, then
# change it back.
wd = os.getcwd()
os.chdir(dirname)
result = os.system('macunpack -f "%s"' % oldname)
if result == 0:
# Unpacking worked, so delete the original.
os.remove(oldname)
os.chdir(wd) # sigh...

os.path.walk(where, _unbin, None)
Is there another (better) way of dealing with this sort of situation?
--
Steven D'Aprano

Feb 28 '07 #6
On Tue, 27 Feb 2007 06:22:51 +0000, Dennis Lee Bieber wrote:

[snip 350-odd file names]
Just a sample of the start of a very long listing...
Thanks for sharing. What's with the nude ferret?

--
Steven D'Aprano

Feb 28 '07 #7
Steven D'Aprano wrote:
On Tue, 27 Feb 2007 20:31:43 -0800, Scott David Daniels wrote:
>> def findallfiles(self, base):
self.results = []
for root,dirs,files in os.walk(base):
os.chdir(root)
^^^ Mistake here, don't change directories during os.walk ^^^

I came across this problem some time ago. I had to walk a directory tree,
calling an external program on each file. Unfortunately, that external
program wrote directly to the current working directory, which caused all
sorts of havoc. This is how I dealt with it:
def unbin(where):
"""Walk through a directory tree, calling macunpack to extract the
contents of MacBinary files.
"""
def _unbin(data, dirname, files):
for oldname in files:
fullname = os.path.normpath(os.path.join(dirname, oldname))
if os.path.isfile(fullname):
# Dammit, macunpack writes directly to the current
# working directory. Changing the cwd breaks the file
# tree walker, so we have to remember the current
# directory, change it to where we want to be, then
# change it back.
wd = os.getcwd()
os.chdir(dirname)
result = os.system('macunpack -f "%s"' % oldname)
if result == 0:
# Unpacking worked, so delete the original.
os.remove(oldname)
os.chdir(wd) # sigh...

os.path.walk(where, _unbin, None)
Is there another (better) way of dealing with this sort of situation?
Does the problem occur if you pass an absolute path to
os.walk()/os.path.walk()?

Peter
Feb 28 '07 #8
On Wed, 28 Feb 2007 08:38:41 +0100, Peter Otten wrote:
Does the problem occur if you pass an absolute path to
os.walk()/os.path.walk()?
Well, arguably the problem is that macunpack insists on writing to the
current working directory.

Or the problem is that os.walk sets the working directory to the initial
argument when you start, and then keeps it until it finishes. (Arguably
that's the right behaviour.)

You can play around with this to check:

def walker(where):
"""Walk through a directory tree, printing the current working directory."
def _walk(data, dirname, files):
print "dirname = '%s'; wd = '%s'" % (dirname, os.getcwd())
os.path.walk(where, _walk, None)
For those times when os.walk's behaviour doesn't mesh well with that of
the external program you are calling (like macunpack) is there an
alternative to:

- save the cwd;
- change directories;
- call the program;
- return to the saved directory

?

--
Steven D'Aprano

Mar 1 '07 #9
Steven D'Aprano wrote:
For those times when os.walk's behaviour doesn't mesh well with that of
the external program you are calling (like macunpack) is there an
alternative to:

- save the cwd;
- change directories;
- call the program;
- return to the saved directory

?
os.walk() creates paths from the root you provide and the files/direcotories
it finds via os.listdir(), and uses that to determine. If that root is an
absolute path any such paths are absolute, too, and undoing the directory
change is not necessary.

cwd = os.getcwd()
for path, dirs, files in os.walk(os.path.abspath(root)):
os.chdir(path)
for file in files:
invoke external program
os.chdir(cwd)

That said, undoing the change immediately is better style, and probably a
good usecase for a with statement like

with current_working_directory(path):
invoke external program

Last minute idea:

for path, dirs, files in os.walk(root):
for file in files:
os.system("cd '%s' && macunpack '%s'" % (path, file))

Peter

Mar 2 '07 #10

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

Similar topics

0
by: Frans Englich | last post by:
Hello, Have a look at this recursive function: def walkDirectory( directory, element ): element = element.newChild( None, "directory", None ) # automatically appends to parent...
4
by: Bill | last post by:
Will IIS6.0 recognize a global.asa file located in a subdirectory on the webserver? I'd like a file that will execute whenever a file in the subdirectory is browsed - or, at least it is...
7
by: KraftDiner | last post by:
The os.walk function walks the operating systems directory tree. This seems to work, but I don't quite understand the tupple that is returned... Can someone explain please? for root, dirs,...
2
by: gregpinero | last post by:
In the example from help(os.walk) it lists this: from os.path import join, getsize for root, dirs, files in walk('python/Lib/email'): print root, "consumes", print sum(), print "bytes in",...
2
by: Martin Marcher | last post by:
Hello, I'm playing around with os.walk and I made up del_tree(path) which I think is correct (in terms of the algorithm, but not as python wants it :)). As soon as some directory is deleted...
2
by: dj | last post by:
Hello All, I am attempting to us os.walk to populate two lists with values from a directory. The first list contains all the files in the directory and subdirectories. The second list contains...
0
by: Jeff McNeil | last post by:
Your args are fine, that's just the way os.path.walk works. If you just need the absolute pathname of a directory when given a relative path, you can always use os.path.abspath, too. A couple...
4
by: Jeff Nyman | last post by:
Greetings all. I did some searching on this but I can't seem to find a specific solution. I have code like this: ========================================= def walker1(arg, dirname, names):...
6
by: D | last post by:
Hello, How can one exclude a directory (and all its subdirectories) when running os.walk()? Thanks, Doug
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.