Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File operation on non-english file name in python

This issue may be faced by many of us but I am poor in unicode handling. Here is the issue: this is a code snippet, I am trying to execute the .exe file and checking whether the file path exists or not but no luck :

#Python 2.6.7

filePath = 'C:\\Test\\'  # Test folder haveing file BitComet_比特彗星_1_25.exe

for (adir, dirs, files) in os.walk(rootdir):
    for f in files:
        path = os.path.join(adir,f)
        if os.path.exists(path ):
            print'Path Found',path 
            #Extract file
            #logging(path )
        else:
            print 'Path Not Found'  
            #logging(path )

I am always getting the result 'Path Not Found'. I tried to use path.decode('utf-8'):
But the script read the file path as:

C:\Test\BitComet_????_1_25.exe    

And since this file path doesn't exist, it goes to the else branch.

Please give me a hint to handle this unicode issue and whether its better if I am able to show user to show the file path on cmd or in log file.

I apologize if this seems to be a duplicate post.

like image 935
Shashi Avatar asked Nov 17 '25 17:11

Shashi


1 Answers

Windows paths are encoding in UTF-16. Python can handle this for you, simply pass a unicode path to os.walk() and you'll get Unicode results instead:

filePath = u'C:\\Test\\'  # Test folder haveing file BitComet_比特彗星_1_25.exe

for (adir, dirs, files) in os.walk(filePath):
like image 180
Martijn Pieters Avatar answered Nov 19 '25 06:11

Martijn Pieters



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!