Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

creating files with utf8 characters on windows

Status
Not open for further replies.

clonny2

MIS
Jan 28, 2003
470
US
I am trying to create some utf8 datasets on both windows and linux using python. I am able to create my datasets with linux just fine, but not windows.

I am able to create a dir with utf8 chars, but not files where the filename has the same utf8 symbol.

Here is my function to create a utf8 file. I pass in the hex value for the begining of the range and the end of the range.

<code>
ef create_utf8_file (path,range_start,range_end):
local_path = path
localRangeStart = range_start
localRangeEnd = range_end
# Create Dirs for each char
for i in range (localRangeStart,localRangeEnd):
local_uchar=unicode(unichr(i))
local_hchar=hex(i)
if os.name == "posix":
local_dir = ('%s/%s-%s' %(local_path,local_hchar,local_uchar))
local_file = ('%s/file-%s-%s' %(local_dir,local_hchar,local_uchar))
#local_file = ('file-%s-%s' %(local_hchar,local_uchar))
if os.name == "nt":
local_dirname = ('%s-%s' %(local_hchar,local_uchar))
encode_dirname = local_dirname.encode('utf-8')
local_dir = ('%s\%s' %(local_path,encode_dirname))
local_filename = ('file-%s-%s' %(local_hchar,local_uchar))
encode_filename=local_filename.encode('utf-8')
local_file = ('%s\%s' %(local_dir,encode_filename))

try:
local_fh = open(local_file, 'w',encoding='utf-8')
local_fh.write("This is hex %s and char %s" %(local_hchar,local_uchar) )
#local_fh.write("Test")
local_fh.close ()
except:
print "Can not create file %s" %local_file
</code>

>---------------------------------------


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top