在python中如何使用os.system()作为变量?
我想获得一个文件名,删除扩展名,并使用字符串作为变量,使用tmdbv3api搜索tvshow在python中如何使用os.system()作为变量?,python,python-3.x,Python,Python 3.x,我想获得一个文件名,删除扩展名,并使用字符串作为变量,使用tmdbv3api搜索tvshow 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
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有一个用于处理路径的库。我会找到其他方法来处理输出,而不是打印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)
您可能最好使用
os.listdir('.')
(对于ls
)、.endswith('.mkv')
)和re
进行替换。虽然如果您想使用子流程方法,您可能会使用subprocess.run
谢谢您的评论,我昨天开始使用python,并通过迁移我的一个bash脚本了解到这一点,但我认为我一直在作弊,没有正确使用python,我一直在使用os.system()对于大多数我觉得舒服的事情,我可能不得不在结尾和结尾处阅读