0

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
1
  • You want program to analyze filenames and group them generating patterns? Well, I can't say it's impossible, but it could be quite a little more complicated than you expect. Commented Aug 4, 2021 at 15:21

2 Answers 2

1

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")
Sign up to request clarification or add additional context in comments.

2 Comments

How can i identify when the file names aren't consecutive? In the example i mentioned there are a couple of numbers missing in between and it split the output into two avoiding the missing file numbers.
? In your example the file-names for "file01_----" and file02_----" are indeed consecutive... if you can't guarantee that you need to check if the file_ids list contains consecutive numbers, then split at the gaps and you should be good to go
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.