When trying to access a file with a Russian unicode character using win32file.CreateFile()
, I get:
Traceback (most recent call last):
File "test-utf8.py", line 36, in <module>
None )
pywintypes.error: (123, 'CreateFile', 'The filename, directory name, or volume l
abel syntax is incorrect.')
Here's the code. I'm using Python 2.7. I verify that I can open it with regular Python 'open':
# -*- coding: UTF-8 -*-
# We need the line above to tell Python interpreter to use UTF8.
# You must save this file with UTF8 encoding.
'''
Testing UTF8 Encoding
'''
import win32file
import os, sys
path = u'C:\\TempRandom\\utf8-1\\boo\\hi это русский end - Copy.txt'
# Clean path when printing since Windows terminal only supports ASCII:
print("Path: " + path.encode(sys.stdout.encoding, errors='replace'))
# Test that you can open it with normal Python open:
normal_fp = open (path, mode='r')
normal_fp.close()
fileH = win32file.CreateFile( path, win32file.GENERIC_READ, \
win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE, \
None, # No special security requirements \
win32file.OPEN_EXISTING, # expect the file to exist. \
0, # Not creating, so attributes dont matter. \
None ) # No template file
result, msg = win32file.ReadFile(fileH, 1000, None)
print("File Content >>")
print(msg)
The solution is to use CreateFileW
and not CreateFile
:
fileH = win32file.CreateFileW
Ironically, the documentation for CreateFile
says it supports PyUnicode strings, but the underlying Windows function does not, unless you use CreateFileW
. CreateFileW
supports wide characters for unicode.
Thanks to this post discussing the C version of CreateFile
:
How do I open a file named 𤭢.txt with CreateFile() API function?