• XSS.stack #1 – первый литературный журнал от юзеров форума

Python script to search for file names

donkey-fuller

RAID-массив
Пользователь
Регистрация
23.02.2022
Сообщения
72
Реакции
5
Hi All,
hope everyone is well? and staying relaxed over the holidays?

All i am a bit stuck trying to write my first python script.
Seeking some advise from the python legends.

Objective: To search a folder with thousands of sub directories, where filenames might contain DVDRip, RIP, and or R5, with file extension of either avi, mkv.

I have working script code if i am only searching for one potential filename includes on of DVDRip, RIP, and or R5, but have to run each separately.

My code is:
import os
import glob
for f in glob.glob('/mnt/folder/Movies/**/*DVD*', '/mnt/folder/Movies/**/*R5*', recursive=True):
print(f)


I thought that if i added an extra comma and another potential name contains it might work. However I get an error i think its is because i am trying to pass to many potential variables where only one can be stated.


Traceback (most recent call last):
File "/home/flapper/py-scripts/test-media-search", line 3, in <module>
for f in glob.glob('/mnt/folder/Movies/**/*R5*', '/mnt/folder/Movies/**/*dvd*', recursive=True):
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: glob() takes 1 positional argument but 2 positional arguments (and 1 keyword-only argument) were given

Would i need to use maybe definining the potential filenames? and or glob.globs rather than glob.glob?

You help would be really appreciated.
 
Python:
import os

for i in os.walk("PATH_TO_DIR"):
    for fn in i[2]:
        if any(m in fn for m in ["DVD", "RIP", "R5"]) and (".avi" in fn or ".mkv" in fn):
            print(os.path.join(i[0], fn))
 
Последнее редактирование:
only searching for one potential filename includes on of DVDRip
using '**/' in start path
Python:
import glob
files = glob.glob('**/mnt/folder/Movies/*RIP*|*R5*', recursive=True)

ИМХО лучше использовать pathlib.
Python:
from pathlib import Path

# find files
files = Path.cwd().glob('filename.*')

# find files with REGex
files = Path.cwd().rglob('**/mnt/folder/Movies/*RIP*|*R5*')
 


Напишите ответ...
  • Вставить:
Прикрепить файлы
Верх