user9262680
user9262680

Reputation: 39

how to count the no of files of each extension in a directory using python?

I'm fairly new to python and i came across this problem,

i want to be able to write a python script that counts the no of files in a directory of each extension and output the following details

  1. First row shows image count
  2. Second row shows file names in padding format.
  3. Third row shows frame numbers continuity

Example:

files in the directory:-

alpha.txt
file01_0040.rgb
file01_0041.rgb
file01_0042.rgb
file01_0043.rgb
file02_0044.rgb
file02_0045.rgb
file02_0046.rgb
file02_0047.rgb

Output:-

1 alpha.txt
4 file01_%04d.rgb 40-43
4 file02_%04d.rgb 44-47

Upvotes: 0

Views: 189

Answers (2)

raphael
raphael

Reputation: 2970

I'd suggest you have a look at python's native Pathlib library (it comes with glob)

here's a first idea how you could do it (sure this can be improved but it should provide you with the basics):

from pathlib import Path
from itertools import groupby

basepath = Path(r"path-to-the-folder")

# get all filenames that follow the pattern
files = basepath.glob("file*_[0-9][0-9][0-9][0-9].rgb")

# extract the pattern so that we get the 2 numbers separately
patterns = [i.stem.lstrip("file").split("_") for i in files]

# group by the first number
groups = groupby(patterns, key=lambda x: x[0])

filenames = list()

# loop through the obtained groups and extract min/max file_ids found
for file_num, group in groups:
    file_ids = [int(i[1]) for i in group]
    filenames.append(f"file_{file_num}_%04d.rgb  {min(file_ids)} - {max(file_ids)}")


print(*filenames, sep="\n")

Upvotes: 1

The Tomster
The Tomster

Reputation: 56

you can use the glob library to search through directories like this

import glob

glob.glob('*.rgb')

This code will return the filenames of all files ending with .rgb in an array for you to sort and edit

Upvotes: 0

Related Questions