Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9033355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:12:38+00:00 2026-06-16T08:12:38+00:00

I have this Python script here that opens a random video file in a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:12:40+00:00Added an answer on June 16, 2026 at 8:12 am

    Just use Unicode literals e.g., u".mp4" everywhere. IO functions in Python will return Unicode filenames back if you give them Unicode input (internally they might use Unicode-aware 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,))
    

    If you want to emulate how your web browser would open video files then you could use webbrowser.open() instead of os.startfile() though the former might use the latter internally on Windows anyway.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some questions about the performance of this simple python script: import sys,
I have a Python script that is running a few ls commands. This script
I have written a short python script that opens Google music in web view
I have a Python script that reads a file (typically from optical media) marking
I wrote this python script to import a specific xls file into mysql. It
I have a python script that needs to be run after each reboot. All
I've got a python script that searches for files in a directory and does
I have a Python script that opens a websocket to the Twitter API and
I'm writing a Python script that takes in a (potentially large) file. Here is
I have a very basic python script that does HTTP connection. import socket def

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.