How to list file paths from multiple directories at once in Phyton
Question:
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:
list.py
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 list.py
@echo off
set dir_1=F:VIDEO
set dir_2=F:Music
python list.py %dir_1% %dir_2%
pause
The Final Result of the txt
output after execution of list.py
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'
Answers:
So that’s main.py
:
#!/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:
files.update(Path(p).rglob(pattern))
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:
f.write('n'.join(results))
Run $ python3 main.py <dir1> <dir2> ... >> file.txt
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:
list.py
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 list.py
@echo off
set dir_1=F:VIDEO
set dir_2=F:Music
python list.py %dir_1% %dir_2%
pause
The Final Result of the txt
output after execution of list.py
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'
So that’s main.py
:
#!/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:
files.update(Path(p).rglob(pattern))
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:
f.write('n'.join(results))
Run $ python3 main.py <dir1> <dir2> ... >> file.txt