使用Python脚本打开一个随机文件

时间:2014-07-21 16:38:54

标签: python random

我有一个Python脚本,我的家人用它在我们的媒体中心播放随机孩子的电视节目。我的妻子告诉我,该节目似乎赞成同样的节目选择。有没有办法让它更随意,以便从一些不同的选项中选择?

提前致谢。

以下是我目前正在使用的内容:

import glob,random,os
files = glob.glob("D:\Recorded TV\Bubble Guppies*.wtv")
files.extend(glob.glob("D:\Recorded TV\Doc McStuffins*.wtv"))
files.extend(glob.glob("D:\Recorded TV\Mickey Mouse Clubhouse*.wtv"))
files.extend(glob.glob("D:\Recorded TV\Octonauts*.wtv"))
files.extend(glob.glob("D:\Recorded TV\Team Umizoomi*.wtv"))
files.extend(glob.glob("D:\Recorded TV\Jake and the Never Land Pirates*.wtv"))
files.extend(glob.glob("D:\Recorded TV\PAW Patrol*.wtv"))
files.extend(glob.glob("D:\Recorded TV\Yo Gabba Gabba*.wtv"))
files.extend(glob.glob("D:\Recorded TV\Henry Hugglemonster*.wtv"))
files.extend(glob.glob("D:\Recorded TV\Wallykazam*.wtv"))
files.extend(glob.glob("D:\Recorded TV\Dora the Explorer*.wtv"))
files.extend(glob.glob("D:\Recorded TV\Jungle Junction*.wtv"))
files.extend(glob.glob("D:\Recorded TV\Little Einstein*.wtv"))
files.extend(glob.glob("D:\Recorded TV\The Wonder Pets*.wtv"))
files.extend(glob.glob("D:\Recorded TV\WordWorld*.wtv"))
file = random.choice(files)
print "Opening file %s..." % file
cmd = "rundll32 url.dll,FileProtocolHandler \"" + file + "\""
os.system(cmd)

1 个答案:

答案 0 :(得分:0)

正如@ paul-seeb所说,一个选择中可能有更多节目。我首先随机挑选一个选择,然后在那里展示你喜欢什么。

selections = [
    'Doc McStuffins',
    'Mickey Mouse Clubhouse',
    ...
    'WordWorld',
]
selection = choice(selections)
shows = glob('D:\Recorded TV\{}*.wtv'.format(selection))
show = choice(shows)