Sam
Sam

Reputation: 439

How to read japanese filename file correctly by python in windows

import os

path="."
dirList=os.listdir(path)

for fileName in dirList:
    print fileName

if the filename is japanese, print to console will be not correct(like ?????.csv,????abc.csv)

open('XXX.csv').readlines()

if the filename is japanese, IOError:No such file or directory: \xe4\xb8\xbcABC.csv

Upvotes: 0

Views: 4540

Answers (1)

Sam
Sam

Reputation: 439

All problems done,thanks

1)if you want to get fileNames which is not English(such as Japanese,Chinese) by os.listdir correctly(not ???.csv) you can add u before your path string listdir doesn't print non-english letters correctly

2)if you want to open a file,you can use file.decode('UTF-8')

#-*- coding: utf-8 -*-
import os

dirList=os.listdir(u"C:\\")

for file in dirList:
    print file
    file2 = file.decode('UTF-8')
    count = len(open('C:\\' + file2).readlines())
    print count 

Upvotes: 1

Related Questions