How to list file paths from multiple directories at once in Phyton


This is my first phyton script. How can I use several different directories at the same time? from which to create the list of files? basically, I adapted this script from another one that worked, but only with one directory.

I tried the option of just nesting one level more.
I am using a batch file with who I send arguments and number of them is very dinamic .. can be 1 argument, 2 or 10 . I need somehow to use sys.argv[1:] to set automatically all dirs from sys.argv[1:] in a list and the list all the file paths from all directories in a single .txt file.

os.walk I found to be closed to my needs

Below is what I managed to do in Phyton script: looking like this:

import fnmatch
import os
import sys
list_of_dirs = sys.argv[1:]

file_list = 'file_list.txt'
sufix = "file '"
prefix = "'"

with open(file_list, 'a') as list_output:
list_output = []
for all_dirs in (list_of_dirs):
     for root, dirnames, filenames in in os.walk(all_dirs):
        for filename in fnmatch.filter(filenames, '*.mp4'):
            list_output.write(os.path.join(sufix+root, filename+prefix)+'n')

list_of_dirs = sys.argv[1:]
looking like this:

['F:\Video', 'F:\Music']

my batch file from where I send the arguments to

@echo off

set dir_1=F:VIDEO 
set dir_2=F:Music
python %dir_1% %dir_2%

The Final Result of the txt output after execution of must be like:

file 'F:VIDEO-Pe-Categorii...03OLZ2R.mp4'
file 'F:VIDEO-Pe-Categorii....0HDVDFZ.mp4'
file 'F:VIDEO-Pe-Categorii....0KOUVIR.mp4'
file 'F:VIDEO-Pe-Categorii...10LOHD5.mp4'
file 'F:VIDEO-Pe-Categorii...1FN7YWE.mp4'
file 'F:VIDEO-Pe-Categorii.....1K5LLNV.mp4'
Asked By: Ionut Bejinariu



So that’s

#!/bin/env python3
import sys
from pathlib import Path
from typing import Set, Iterable

def get_files(paths: Iterable[str], pattern: str = '*.mp4') -> Set[Path]:
    files = set()
    for p in paths:
    return files

if __name__ == '__main__':
    dirs = sys.argv[1:]
    results = list(map(lambda f: f"file '{f}'", get_files(dirs)))
    print(*results, sep='n')
    with open('file.txt', 'w') as f:

Run $ python3 <dir1> <dir2> ... >> file.txt

Answered By: kupihleba
Categories: questions Tags:
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.