python 批量修改文件名

小灰灰 2021-09-17 14:20 602阅读 0赞
  1. import os
  2. # with open("shijie_small_youtu_align_failed.txt","r") as face_file:
  3. # for line in face_file.readlines():
  4. # srcpath = line.strip().split("/")
  5. # dstpath = ""
  6. # num = 0
  7. # for path in srcpath:
  8. # if num == 5:
  9. # path = "shijie_small_youtu_MTCNN_Alignment_Margin32"
  10. # num += 1
  11. # dstpath += path
  12. # dstpath += "/"
  13. # dstpath = dstpath[0:-1]
  14. # print(line[0:-1])
  15. # print(dstpath)
  16. # cmd = "cp " + dstpath + " " + line[0:-1]
  17. # os.system(cmd)
  18. def findAllfile(path, allfile):
  19. filelist = os.listdir(path)
  20. for filename in filelist:
  21. filepath = os.path.join(path, filename)
  22. if os.path.isdir(filepath):
  23. #print(filepath)
  24. findAllfile(filepath, allfile)
  25. else:
  26. allfile.append(filepath)
  27. return allfile
  28. #mv /DATA/share/publicdata/stars/zhouxun/u=3942420804,889887287&fm=27&gp=0.jpg /DATA/share/publicdata/stars/zhouxun/7_zhouxun_8.jpg
  29. clusterpath = "/DATA/share/publicdata/stars"
  30. filelist = os.listdir(clusterpath)
  31. floder_ID_index = 0
  32. for filename in filelist:
  33. filepath = os.path.join(clusterpath, filename)
  34. if os.path.isdir(filepath):
  35. print(filepath)
  36. floder_ID_index += 1
  37. print(floder_ID_index)
  38. filepath_split = filepath.strip().split("/")
  39. floder_ID = filepath_split[-1]
  40. image_files = findAllfile(filepath,[])
  41. #print(image_files)
  42. face_index = 1
  43. for image in image_files:
  44. dstname = clusterpath + "/" + floder_ID + "/" + str(floder_ID_index) + "_" + floder_ID + "_" + str(face_index) + ".jpg"
  45. image = "'" + image + "'"
  46. print(image)
  47. print(dstname)
  48. face_index += 1
  49. cmd = "mv " + image + " " + dstname
  50. os.system(cmd)

发表评论

表情:
评论列表 (有 0 条评论,602人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python 批量修改文件名

    > 仅个人需求,有需要的可以自取。 前段时间为家里孩子下载了一批课程,但是文件命名就很奇怪也很乱,就想着将文件名修改掉便于查看。 这批视频下载下来后前边都给了诸如`001`