2022年 11月 5日

python 中文路径读取

错误:UnicodeEncodeError: ‘ascii’ codec can’t encode character ‘\u7ca4’ in position 96: ordinal not in ran

files = os.listdir(path.encode('utf-8'))
copyfile((path+fi).encode(),(new_file).encode())
  1. import numpy as np
  2. from shutil import copyfile
  3. import cv2
  4. import os
  5. j = 1
  6. path = r'/data/lyy/sign_detection/dataset_sign/raw_data/20210526/1622020717794/'
  7. newpath = r'/data/lyy/sign_detection/dataset_sign/raw_data/20210526/11/'
  8. files = os.listdir(path.encode('utf-8'))#.decode())
  9. for i, fi in enumerate(files):
  10. print(fi)
  11. fi = fi.decode()
  12. # print(fi)
  13. # path = path.encode('utf-8')
  14. PATH = os.path.join(path,fi)
  15. print(PATH)
  16. # img = cv2.imread(PATH)
  17. # cv2.imshow('img',img)
  18. # cv2.waitKey(0)
  19. #==============txt rename===============================
  20. # with open(PATH, 'r',encoding='utf-8') as file :#, open(newpath+fi,'w') as filenew: ,encoding='UTF-8-sig' , encoding='utf-8'
  21. # # file = pp.readlines()
  22. # # if fi == "20200822210255_003500_1_3_3.txt":
  23. # # print(fi)
  24. # # for filelines in file.readlines():
  25. # # if filelines == '\n':
  26. # # break
  27. # # lines = filelines.split(' ')
  28. # # fi = fi.decode()
  29. # # path = path.decode()
  30. # filename = fi.split('_')
  31. # box_str = filename[0] + '_'+filename[1]+ '_'+filename[-1]
  32. # # box = box_str.replace(',','-')
  33. #
  34. # new_file = newpath + box_str
  35. # copyfile(path+fi,new_file)
  36. #============img rename=========================
  37. filename = fi.split('_')
  38. box_str = filename[0] + '_' + filename[1] + '_' + filename[-1]
  39. # box = box_str.replace(',','-')
  40. # os.rename(os.path.join(PATH), os.path.join(newpath, box_str))
  41. # j += 1
  42. new_file = newpath + box_str
  43. copyfile((path+fi).encode(),(new_file).encode())