0
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

Sam
  • 409
  • 1
  • 7
  • 16
  • what about: http://stackoverflow.com/questions/3089700/python-not-opening-japanese-filenames – jmunsch Feb 27 '14 at 03:09
  • The python2 libraries are terrible when it comes to unicode support, some libs have backward compatible "workarounds" but some other libs (like subprocess) don't. – pasztorpisti Feb 27 '14 at 03:28
  • fileName = fileName.decode("UTF-8")(from jmunsch's link) still IOError, change file name to english is OK – Sam Feb 27 '14 at 03:29
  • Try http://stackoverflow.com/questions/1052225/convert-python-filenames-to-unicode – Mark Ransom Feb 27 '14 at 04:40
  • fileName2 = fileName.decode("UTF-8") open('fileName2').readlines() it works.But fileName must Japanese string, I can not get the correct fileName(become ???.csv) by os.listdir(),must hard code fileName.This question can be summarized to "how to get correct japanese fileName by os.listdir()" – Sam Feb 27 '14 at 05:45

1 Answers1

1

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 
Community
  • 1
  • 1
Sam
  • 409
  • 1
  • 7
  • 16