使用 Python 脚本打开一个随机文件 [英] Open a random file with Python Script

查看:88
本文介绍了使用 Python 脚本打开一个随机文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Python 脚本,我的家人用它来在我们的媒体中心播放随机的儿童电视节目.我的妻子告诉我,该节目似乎偏爱相同的节目选择.有没有办法让它更随机,以便它从一些不同的选项中选择?

I have a Python Script that my family uses to play a random kid's TV show on our Media Center. My wife tells me that the program seems to favor the same selection of shows. Is there a way to make it more random so that it picks from some different options?

提前致谢.

这是我目前使用的:

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)

推荐答案

正如@paul-seeb 所说,一个选择中可能有更多程序.如果你喜欢的话,我会先随机挑选一个选择,然后在那里展示.

As @paul-seeb said, there are probably more programs in one selection. I'd first randomly pick a selection and then a show there if that's what you prefer.

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

这篇关于使用 Python 脚本打开一个随机文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆