简体   繁体   中英

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)

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM