lll
lll

Reputation: 315

Python Directory traveling os.path

I try to create a program which can recursively traverse multiple directories and print the file listing in hieararchical way like :

Folder
----x.c
----x.bin
----Folder
---------x.c

I try to do with program like (with file/folders detail) :

#!/usr/bin/python

import os
for item in os.listdir(".") :
        if os.path.isdir(item) :
                print "-" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        elif os.path.isfile(item) :
                print item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print "Nothing \n"

But i can't go in any directory i try with like (A is a directory here) :

#!/usr/bin/python

import os
for item in os.listdir(".") :
        if os.path.isdir(item) :
                print "-" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        elif os.path.isfile(item):
                print item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print "Nothing"

for item in os.listdir("A") :
        if os.path.isdir("A") :
                print "-" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)

        elif os.path.isfile("A") :
                print "--" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print "Nothing"

The listing is wrong i don't understand why i can't just go from . to A and how to do it .And worst if i go on B (the second folder here):

#!/usr/bin/python

import os
for item in os.listdir(".") :
        if os.path.isdir(item) :
                print "-" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        elif os.path.isfile(item):
                print item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print "dunno"

for item in os.listdir("A") :
        if os.path.isdir("A") :
                print "-" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)

        elif os.path.isfile("A") :
                print "--" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print "lulz"

for item in os.listdir("A/B") :
        if os.path.isfile("A/B") :
                print "---" + item + '\t' + str(os.stat(item).st_size) + "kb" + '\t' + str(os.stat(item).st_atime)
        else :
                print 'Nothing'

Upvotes: 1

Views: 1332

Answers (2)

Chris Doggett
Chris Doggett

Reputation: 20757

Borrowed a bit from this answer: List directory tree structure using Python

import os

def list_files(path, spaceChar=' ', spaceWidth=4):
    for root, dirs, files in os.walk(path):
        level = root.replace(path, '').count(os.sep)
        indent = spaceChar * (spaceWidth * level)
        print('{}{}/'.format(indent, os.path.basename(root)))
        subindent = spaceChar * spaceWidth * (level + 1)
        for f in files:
            print('{}{}'.format(subindent, f))

list_files(".", "-", 3)

Upvotes: 0

Michael
Michael

Reputation: 2261

I think you want to use os.walk

for (cur, dirs, files) in os.walk('.'):
    pass

This will give you the current directory, a list of directories in the current directory and a list of files in the current directory.

I think you want something like

for (cur, dirs, files) in os.walk('.'):
    depth = len(cur.split('/'))
    print "--" * depth, cur
    for fname in files:
        print "--" * (depth + 1), fname

Upvotes: 1

Related Questions