python 我怎样才能知道我的档案里有多少集电影

qxgroojn  于 2023-03-21  发布在  Python
关注(0)|答案(1)|浏览(110)

我想知道我的文件里有多少集的电影。剧集的名字是这样的:“name. S 01 E01. 720 p.WEB-DL.mkv”,“name.S01E02...”我想我应该使用这样的东西,但idk下一步是什么

import os
with os.scandir(path) as files:
    for file in files:
    ...

但是每部电影都有一个特定的名称(例如)“name.Joint.Economic.Area.S01E01.720p.NF.WEBRip.x264-GalaxyTV”或其他一些东西,我想写一个代码,可以使用任何类型的名称
我认为我这样做的方式不是专业的方式。
我用了这个:

import os
with os.scandir(path) as files:
    for file in files:
        num = 01
        if any(str(num) in file.name):
            print("yes")

但我得到了一个错误。

0yg35tkg

0yg35tkg1#

也许这可以帮助:

#!/usr/bin/env python3
import glob
import os,sys
import re
import json
try:
  movie_path_orig = sys.argv[1]
except:
  print(f"use ./{sys.argv[0]} <movie path>")
  sys.exit(0)

movie_path = os.path.join(movie_path_orig, "*.mkv")

print(f"Processing {movie_path}..")
files = glob.glob(movie_path)

summary = {}
for f in files:
  r=re.search("^(.*)S([0-9]{2})E([0-9]{2})(.*)", f)
  prefix = r.group(1)
  s = int(r.group(2)) #season
  e = int(r.group(3)) #episode
  suffix = r.group(4)
  if prefix not in summary.keys():
    summary[prefix] = { 'seasons': { f"{s}": e }, 'prefix': prefix, 'suffix': suffix}
  else:
    if f"{s}" not in summary[prefix]['seasons'].keys():
       summary[prefix]['seasons'][f"{s}"] = e
    else:
       summary[prefix]['seasons'][f"{s}"] = max(summary[prefix]['seasons'][f"{s}"], e)

print(json.dumps(summary, sort_keys=True, indent=4))

for i in summary.keys():
  for season in summary[i]['seasons'].keys():
    for episode in range(1, summary[i]['seasons'][season]+1):
      s = "{:02d}".format(int(season))
      e = "{:02d}".format(episode)
      file2chk = f"{summary[i]['prefix']}S{s}E{e}{summary[i]['suffix']}"
      if not os.path.exists(file2chk):
          print(f"missing {file2chk}")

首先,你需要知道每部电影每一季的最后一集(第一集for),然后检查从第01集开始的所有内容(第二集for)。只要确保你有最后一集。
对于具有以下内容的电影目录:

name.Joint.Economic.Area.S01E01.720p.NF.WEBRip.x264-GalaxyTV.mkv
name.Joint.Economic.Area.S02E03.720p.NF.WEBRip.x264-GalaxyTV.mkv

它输出:

Processing movies/*.mkv..
{
    "movies/name.Joint.Economic.Area.": {
        "prefix": "movies/name.Joint.Economic.Area.",
        "seasons": {
            "1": 1,
            "2": 3
        },
        "suffix": ".720p.NF.WEBRip.x264-GalaxyTV.mkv"
    }
}
missing movies/name.Joint.Economic.Area.S02E01.720p.NF.WEBRip.x264-GalaxyTV.mkv
missing movies/name.Joint.Economic.Area.S02E02.720p.NF.WEBRip.x264-GalaxyTV.mkv

相关问题