简体   繁体   中英

Python: List drives, files in subdirectories and sort by extension

I'm trying to write codes as per subject for a project however encounter error. Found a few scripts and merged them but apparently didn't work out as I though it would be.

Basically the objective is to:

  1. List all drives
  2. List all files in the drives and sub-directories
  3. Thereafter sort it by its extension

Here are my complied codes:

import os
import os.path
dr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
drives = ['%s: '%d for d in dr if os.path.exists('%s: '%d)]

def convert(list):
    return tuple(list)

listdrives = convert(drives)

for root, dirname, files in os.walk(listdrives):
    for x in files:
        x.sort(key=lambda f: os.path.splitext(f)[1])

print(x)

Here is the error message:

Traceback (most recent call last):
  File "d:/WORK/testlist.py", line 11, in <module>
    for root, dirname, files in os.walk(listdrives):
  File "C:\Users\MHKHR\AppData\Local\Programs\Python\Python38-32\lib\os.py", line 339, in walk
    top = fspath(top)
TypeError: expected str, bytes or os.PathLike object, not tuple

I would be glad if anyone could take a look at it! Thanks!

EDIT

In addition, I encounter a syntax error when exporting to xlsx. I just couldn't figure out why

import os
import os.path
import xlsxwriter

dr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
drives = ['%s:/'%d for d in dr if os.path.exists('%s:' % d)]

for drive in drives:
    for res in os.walk(drive):
        root, directories, files = res
        
        files.sort(key=lambda f: os.path.splitext(f)[1])
        files = [os.path.join(root, f) for f in files]

        return directories
        return files

workbook = xlsxwriter.workbook('D:\\WORK\\test.xlsx')
worksheet = workbook.add_worksheet()

#set column length
worksheet.set_column('A:A', 80)
worksheet.set_column('B:B', 80)

#write headers
worksheet.write('A1', 'Directories')
worksheet.write('B1', 'Files')

dirts = (directories)
fil = (files)

for d1 in range(len(dirts)):
    worksheet.write(d1+1, 0, dirts[d1])
    worksheet.write(d1+1, 1, fil[d1])

workbook.close()

Here's something that has worked locally on my Windows machine:

import os
import os.path
dr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
drives = ['%s:/'%d for d in dr if os.path.exists('%s:' % d)]

for drive in drives:
    for res in os.walk(drive):
        root, directories, files = res
        
        files.sort(key=lambda f: os.path.splitext(f)[1])
        files = [os.path.join(root, f) for f in files]

        print(directories)
        print(files)

Your initial error that led to the traceback in your question was due to the fact that you can't os.walk on a list, you have to os.walk on a directory path and walk on each drive.

I had an issue with using the backslash for the drives variable, play around with which works.

You also shouldn't call sort on an individual object in a list, rather the entire list object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM