Dave Melia
Dave Melia

Reputation: 317

Unable to list files with Python for-loop

I have just broken away from LPTHW and I'm trying to get my hands dirty with a renaming script.

I am having problems trying to list the files within a directory, the code is:

def rename_files():
    for root, dirs, files in os.walk(path):
        dirs_len = len(dirs)
        for i in range(0, dirs_len):
            print dirs[i]

The above code will correctly display a list of directories as expected.. However, doing something like this:

    for root, dirs, files in os.walk(path):
        dirs_len = len(dirs)
        for i in range(0, dirs_len):
            print dirs[i]
            print files

will result in an empty list under each directory.

I experimented by creating a files_len and placing it within the nested for-loop:

    for root, dirs, files in os.walk(path):
        dirs_len = len(dirs)
        files_len = len(files)
        for i in range(0, dirs_len):
            print dirs[i]
            print files_len

it results in 0.

If I place the files_len within the first for-loop, it results in 3, which is the correct number of files within a directory.

I wanted it to print something like:

My Pictures

And I just cannot figure out how to structure my code to make it behave as I want it to.

Can you point me in the right direction, please?

Many thanks and much appreciated!

-EDIT- The ultimate goal here is to look in each directory, take in a raw_input to change the name of a file, once the last file is reached, move on to the next directory, rinse and repeat.

For example:

Dir1

-> Rename img1.png: img_1.png (<-- raw_input)

-> Rename img2.png: img_2.png (<-- raw_input)

-> Rename img3.png: img_3.png (<-- raw_input)

Dir2

-> Rename img1.png: img_1.png (<-- raw_input)

-> Rename img2.png: img_2.png (<-- raw_input)

-> Rename img3.png: img_3.png (<-- raw_input)

And so on until the last file in the last directory is reached.

Upvotes: 2

Views: 81

Answers (3)

Nico Ul
Nico Ul

Reputation: 127

The os.walk function goes through all of the folders starting from the top. You won't have to manually iterate the 'dirs', that it returns, as it's just a list of the dirs in the current 'root' folder, while 'files' is a list of files in the current 'root' folder.
Therefore, this should do what you want:

for root, dirs, files in os.walk( path ):
    print( "Root: %s\nTotal Files:%i" % ( root, len( files )))
    for file in files:
        print( "\t%s" % file )

Upvotes: 0

Marcin Zdunek
Marcin Zdunek

Reputation: 1011

This code works good for me:

import os

path = "D:\\myfolder"

for root, dirs, files in os.walk(path):
    print "root:", root
    for file in files:
        print "-", file

Upvotes: 0

Yoav Glazner
Yoav Glazner

Reputation: 8066

You may try something like this

for root, dirs, files in os.walk(path):
    print("%s:" % root)
    for f in files:
        print ("-> %s" % f)

This will output

My Pictures:

->img1.png

->img2.png

SomeOtherFolder:

->someotherfile.txt

...

Upvotes: 2

Related Questions