Home > Blockchain >  Getting Detailed Information About Folders And Their Files
Getting Detailed Information About Folders And Their Files

Time:05-06

I'm trying to os.walk() through 5 folders containing mp3 files. I want to display the timestamps of each folder and file as well as their sizes and of course names.

With

for x in os.walk(os.getcwd()):
   print(x)

I am able to get the folder and filenames, but don't know how to iterate through them as each folder is one tuple. I want to assign each tuple to a different variable to access them better, but I don't know how to do that while in a for loop.

Any ideas how to do that? Or how to accomplish what I'm trying to do in a better way? Thanks!

CodePudding user response:

You need two for loops to access the individual files:

for dirpath, dirnames, filenames in os.walk( os.getcwd() ):
    for filename in filenames:
        # your code here
        # be aware that 'filename' is not the relative path to the file
        # you can get that by concatenating 'dirpath' and 'filename' together

dirpath refers to the current path in the walk

dirnames is a list of the directories in dirpath

filenames is a list of the files in dirpath

  • Related