Python 2.5.2: trying to open files recursively

Posted by user248959 on Stack Overflow See other posts from Stack Overflow or by user248959
Published on 2010-04-05T11:14:21Z Indexed on 2010/04/05 11:23 UTC
Read the original article Hit count: 408

Filed under:
|

Hi,

the script below should open all the files inside the folder 'pruebaba' recursively but i get this error:

Traceback (most recent call last):
File "/home/tirengarfio/Desktop/prueba.py", line 8, in f = open(file,'r') IOError: [Errno 21] Is a directory

This is the hierarchy:

pruebaba
  folder1
    folder11
       test1.php
    folder12
       test1.php
       test2.php
  folder2
    test1.php

The script:

import re,fileinput,os

path="/home/tirengarfio/Desktop/pruebaba"
os.chdir(path)
for file in os.listdir("."):

    f = open(file,'r')

    data = f.read()

    data = re.sub(r'(\s*function\s+.*\s*{\s*)',
            r'\1echo "The function starts here."',
            data)

    f.close()

    f = open(file, 'w')

    f.write(data)
    f.close()

Any idea?

Regards

Javi

© Stack Overflow or respective owner

Related posts about python

Related posts about newbie