1

I have the following code:

path = "/path/to/directory/"
for dirpath, dirnames, filenames in os.walk(path):
    for filename in filenames:
        file = os.path.join(dirpath, filename)
        folder = os.path.basename(dirpath)
        filesize = os.path.getsize(file)

The only problem is that I would like to filter and get the information only for the mp4 files, excluding the others from the search.

I tried to add *.mp4 at the end of path without success, how I can do that?

sj95126
  • 6,520
  • 2
  • 15
  • 34
Paul Mark
  • 189
  • 12

1 Answers1

2

os.walk() requires a single directory argument, so you can't use wildcards. You could filter the contents of filenames but it's probably easier to just do this:

path = "/path/to/directory/"
for dirpath, dirnames, filenames in os.walk(path):
    for filename in filenames:
        if not filename.endswith(".mp4"):
            continue
        file = os.path.join(dirpath, filename)
        folder = os.path.basename(dirpath)
        filesize = os.path.getsize(file)

Alternatively, you could use the more modern and preferred pathlib; this will find all .mp4 files recursively:

from pathlib import Path

path = "/path/to/directory/"
for file in Path(path).rglob("*.mp4"):
    [....]

Each file object will have attributes and methods you can use to obtain information about the file, e.g. file.name, file.stat(), etc.

sj95126
  • 6,520
  • 2
  • 15
  • 34
  • Thanks for the pathlib suggestion, but I'm getting this error: ```ImportError: cannot import name 'Path' from partially initialized module 'pathlib' (most likely due to a circular import)``` – Paul Mark Jul 22 '22 at 14:16
  • This is usually because of a name conflict between your script and a Python module. See [this answer](https://stackoverflow.com/questions/68393604/attributeerror-partially-initialized-module-datetime-has-no-attribute-dateti) for example. – sj95126 Jul 22 '22 at 14:40