Welcome, guest | Sign In | My Account | Store | Cart

This code is find and print file extensions from directory.

Python, 14 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
import sys
import os

path = os.path.abspath('.') # default is current dir
if len(sys.argv) == 2:
    path = sys.argv[1]
exts = []
for root, dirs, files in os.walk(os.path.expanduser(path)):
    for fn in files:
        bn, ext = os.path.splitext(fn)
        if not ext in exts:
            exts.append(ext)
            if ext:
                print ext

Today, we too use time for clean-up files today. It is good to use the file maintenance software made in a search tool OS standard (or 3rd party software), It can checks file extension exists by this code.

Created by Keisuke URAGO on Thu, 17 Apr 2014 (MIT)
Python recipes (4591)
Keisuke URAGO's recipes (12)

Required Modules

  • (none specified)

Other Information and Tasks