Python how to read dir and filenames?

Reading dir and files name on Win 10 - 64 ?

normally i use TXT file or py file all using the US character map
and no problems to read any dir of file name

but sometimes i receive files from someone else but they use French character set

is there a simple way to read the 2 character set in python ?

and is it possible to mix the 2 set in TXT file ?

thanks for any feedback
happy bl

here is code for my little script

> Blockquote

import bpy

import os

filewnam = ''
fw = open(filewnam, "a")

fp =
path1 = os.path.dirname(fp)
print ('path1 = ', path1 )
print ()

print ()
print (  ' files =', os.listdir(path1)  )
print ()

print ()
print (' ###   ######### ' )
print ()

import os
#path = "C:\\Blender\\2.79\scripts\\addons_contrib\\presets"
path = path1

for (dirpath, dirnames, filenames) in os.walk(path):

	print ("dirpath:", dirpath)
	print ("filenames:",filenames)

print ()
print (' Walk path' )
print ()
print ()
ndir = 0
totfile1 = 0
for (dirpath, dirnames, filenames) in os.walk(path):

	print ("dirpath:", dirpath)
	# Write names to file 
	t1 =  dirpath
	fw.write("%s\n" % t1)
	fw.write('\n')  		# adds empty line
#   fw.write('\n')  		# adds empty line
	ndir +=1			# # of dirs
	jjf1 = 1
	for f1 in filenames:
		extension = os.path.splitext(f1)
		filename, file_extension = os.path.splitext(f1)
#   	print ('extension = ', file_extension)
#   	print ('len =', len (file_extension))
		if file_extension == '.jpg':
			print ('files ', jjf1 , '= ',f1)

		# Write  file names 
			t1 = str(jjf1) + " , " + f1
			fw.write("%s\n" % t1)
			totfile1 +=1
	fw.write('\n')  		# adds empty line
	print ()
print ()
print ('# of dir ', ndir )
print ()
print ('# of files ', totfile1 )
t1 = '# of dir ' + str(ndir) 
fw.write("%s\n" % t1)
fw.write('\n')  		# adds empty line
t1 = '# of files ' + str(totfile1) 
fw.write("%s\n" % t1)


It sounds like you’re having some basic Unicode issues. You should be able to set your text file’s encoding to UTF-8 in any good programmer’s text editor, which will allow you to mix all sorts of characters from different languages. In Python 3+ “strings” are now Unicode and old style 255 character ASCII is now “bytes”, meaning that Unicode is basically the default assumption.

Hope some of this helps!

p.s. you might want to edit your post and put your code between two sets of triple backticks (```) so that it’s readable as a single pre-formatted block with a fixed-width font.

I was looking for that code thing will try to correct first post
code has been corrected in first post !

i’m using notepad 2 I think there is a way to make it UTF-8
is there an example of this in the template of blender !

so if I do that then it should be able to decode words in French or std ASCII ?

I mean I reading files name and DIR
I think normally on Win you are not supposed to use accent only std ASCII
but not certain about that !

happy bl

I don’t think I completely understand the problem you’re encountering (is there an error message?), but I can point out some relevant references that might help:

NTFS filesystems (default for Windows 10) store the filenames as Unicode, whereas the older FAT systems don’t.

Python 2 used to have problems with accessing Unicode filesystems, but for Python 3 it should be “normal” since all strings are Unicode unless you specifically create a “bytes” object. Blender switched from Python 2 to Python 3 in Blender version 2.5, so unless you’re using an older version of Blender, your Python should be Unicode friendly by default.

Happy Blending!

I receive files with some accent - French names
and when I read in blender and python 3
I got a Unicode error

so addon was having problem reading the French name on win

hope you see the problem

happy bl