简体   繁体   中英

How to find the extension of all files in a directory and all its sub-directories?

I'm trying to list the extension (ie, type) of all the files in a directory and all its sub-directories. I have successfully listed all the file names and here's my code:

import os
path = os.getcwd()
for dirpath, dirnames, filenames in os.walk(path):
    for file in filenames:
        file_path = os.path.join(dirpath, file)
        file_size = os.path.getsize(file_path)
        print("{} : {}".format(file_path, round(file_size, 3)))
    for dirname in dirnames:
        dir_path = os.path.join(dirpath, dirname)
        dir_size = os.path.getsize(dir_path)
        print("{} : {}".format(dir_path, round(dir_size, 3)))

I want to get the extension (ie, type) of all the files in the directory and all its sub-directories. Any help will be appreciated!

This will also give you the unique extensions found in the directory and its subdirectories

import os
path = os.getcwd()
extensions = set()

for dirpath, dirnames, filenames in os.walk(path):
    for file in filenames:
        file_extension = os.path.splitext(file)[1]
        extensions.add(file_extension)

print("Extensions found:", extensions)

Here's how you can extract the file extension using regular expressions.

\.([^.]*)$ means "at the end of the string, match a dot followed by anything that is not a dot, and save the non-dot part to the result".

Note that if a file has no extension - such as the Windows hosts file - the match will be None , in which case we need to replace it with an empty string.

import os
import re

prog = re.compile(r'\.([^.]*)$')

path = os.getcwd()

for dirpath, dirnames, filenames in os.walk(path):
    for file in filenames:
        file_path = os.path.join(dirpath, file)
        m = prog.search(file)
        file_ext = m.group(1) if m else ''
        file_size = os.path.getsize(file_path)
        print("{} : {} : {}".format(file_path, file_ext, round(file_size, 3)))

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