Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
84 views
in Technique[技术] by (71.8m points)

How can I use os.system() as a variable in python?

I want to get a filename remove the extension and use the string as a variable to search for tvshow using tmdbv3api

someshow = os.system("ls ./ | grep '.mkv' | sed 's/.{3}$// ; s/./ /g'")


from tmdbv3api import TMDb
from tmdbv3api import TV
tmdb = TMDb()

tv = TV()
show = tv.search(someshow)

for result in show:
   print(result.id)
question from:https://stackoverflow.com/questions/65933828/how-can-i-use-os-system-as-a-variable-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Python has a library just to deal with paths. I would find some other way to deal with the output than to print the id.

from pathlib import Path
from tmdbv3api import TMDb
from tmdbv3api import TV
tmdb = TMDb()

tv = TV()

mkv_files = Path('.').glob('*.mkv')
for mkv_file in mkv_files:
    show = tv.search(mkv_file.stem)
    for result in show:
        print(result.id)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...