tags:

views:

44

answers:

1

I want to convert this GNU command into a python function:

find folder/ 2>/dev/null > file.txt

The find will list all files and folders from the directory recursively and write them to a file.

What I have now in Python is:

import os
project="/folder/path"
i=0
for (project, dirs, files) in os.walk(project):
   print project
   print files
   i += 1

But now I am trying to make the output exactly as find does.

+1  A: 
import os
path = "folder"
for (dirpath, dirnames, filenames) in os.walk(path):
    print dirpath
    for filename in filenames:
        print os.path.join(dirpath, filename)

Instead of print you can write to file.

Messa
Thank you, this is the correct solution. I was using print projectif len(files) > 0: print filesi += 1but it wasnt joining them.Thank you.
Dragos
One more thing please, some files are not r-x by everyone or other errors while reading, how can I prevent in Python their inclusion ? (2>/dev/null from find)
Dragos