python Error in Reading Chinese Path of 2 Solutions

  • 2021-09-24 23:15:18
  • OfStack

Encoding problems may cause errors when python reads Chinese paths

Solution 1: Split the path and encode it separately


import os 
root_path = 'E:\\project\\sk_man-master\\SK\\static\\sk\\new_clothes\\'+u' Trousers '
  for file in os.listdir(root_path):
 print file.decode('gbk')

Method 2: Encode all paths in unicode format


root_path = unicode('E:\\project\\sk_man-master\\SK\\static\\sk\\new_clothes\\ Trousers ','utf-8')

Supplement: Error reading word path from python

python reading doc document is not convenient, so the link above doc file is transferred to docx, but when I use this code, the path 1 straight error will report 1 some strange errors

pywintypes. com_error: (-2147023174, 'rpc server unavailable. ', none, none)

The query learned that this is just a garbled code indicating that the path is unavailable

But after trying to write various paths, I found that there were mistakes.

Then the method of os. path. abspath is used to obtain the absolute path


rootdir = 'E:\python project\ English 6 Grade ' 
def doSaveAas():
  word = wc.Dispatch('Word.Application')
  out_name = filename.replace("doc", r"docx")
  in_file = os.path.abspath(rootdir + "\\" + filename)
  out_file = os.path.abspath(rootdir + "\\" + out_name)
  doc = word.Documents.Open(in_file) #  Files under the destination path 
  doc.SaveAs(out_file, 12, False, "", True, "", False, False, False, False) #  Files under the converted path 
  doc.Close()
  word.Quit()

It will be fine after this modification


Related articles: