2

I have this Python script here that opens a random video file in a directory when run:

import glob,random,os  
files = glob.glob("*.mkv")  
files.extend(glob.glob("*.mp4"))  
files.extend(glob.glob("*.tp"))  
files.extend(glob.glob("*.avi"))  
files.extend(glob.glob("*.ts"))  
files.extend(glob.glob("*.flv"))  
files.extend(glob.glob("*.mov"))  
file = random.choice(files)  
print "Opening file %s..." % file  
cmd = "rundll32 url.dll,FileProtocolHandler \"" + file + "\""  
os.system(cmd)

Source: An answer in my Super User post, 'How do I open a random file in a folder, and set that only files with the specified filename extension(s) should be opened?'

This is called by a BAT file, with this as its script:

C:\Python27\python.exe "C:\Programs\Scripts\open-random-video.py" cd   

I put this BAT file in the directory I want to open random videos of.

In most cases it works fine. However, I can't make it open files with Unicode characters (like Japanese or Korean characters in my case) in their filenames.

This is the error message when the BAT file and Python script is run on a directory and opens a file with Unicode characters in its filename:

C:\TestDir>openrandomvideo.BAT

C:\TestDir>C:\Python27\python.exe "C:\Programs\Scripts\open-random-video.py" cd
The filename, directory name, or volume label syntax is incorrect.

Note that the filename of the .FLV video file in that log is changed from its original filename (소시.flv) to '∩╗┐' in the command line log.

EDIT: I learned that the above command line error message is due to saving the BAT file as 'UTF-8 with BOM'. Saving it as 'ANSI or UTF-16' shows the following message instead, but still does not open the file:

C:\TestDir>openrandomvideo.BAT

C:\TestDir>C:\Python27\python.exe "C:\Programs\Scripts\open-random-video.py" cd
Opening file ??.flv...

Now, the filename of the .FLV video file in that log is changed from its original filename (소시.flv) to '??.flv.' in the command line log.

I'm using Python 2.7 on Windows 7, 64-bit.

How do I allow opening of files that have Unicode characters in their filenames?

4

4 回答 4

3

只需使用 Unicode 文字,例如,u".mp4"无处不在。如果你给它们 Unicode 输入,Python 中的 IO 函数将返回 Unicode 文件名(在内部,它们可能使用支持 Unicode 的 Windows API):

import os
import random

videodir = u"." # get videos from current directory
extensions = tuple(u".mkv .mp4 .tp .avi .ts .flv .mov".split())
files = [file for file in os.listdir(videodir) if file.endswith(extensions)]
if files: # at least one video file exists
    random_file = random.choice(files)
    os.startfile(os.path.join(videodir, random_file)) # start the video
else:
    print('No %s files found in "%s"' % ("|".join(extensions), videodir,))

如果您想模拟您的网络浏览器如何打开视频文件,那么您可以使用webbrowser.open()而不是os.startfile()尽管前者可能会在 Windows 内部使用后者。

于 2012-12-25T15:17:17.960 回答
2

运行BAT文件时的错误是因为BAT文件本身保存为“UTF-8 with BOM”。“∩╗┐”字节不是损坏的文件名,它们是存储在 BAT 文件中的字面第一个字节。将 BAT 文件重新保存为 ANSI 或 UTF-16,这是 BAT 文件支持的唯一编码。

于 2012-12-25T19:46:21.043 回答
0

要么使用 JF Sebastian 描述的 Unicode 文字,要么使用始终使用 Unicode 的 Python 3。

(对于 Python 3,您的脚本需要稍作修改:print 现在是一个函数,因此您必须在参数列表周围加上括号。)

于 2012-12-25T18:29:26.340 回答
-1

请熟悉自己添加# -*- coding: utf-8 -*-源代码,

所以python了解你的unicode。

于 2012-12-25T15:36:51.097 回答