程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python] how to batch name a file name as four or five digits

編輯:Python
for i in range(99):
change_i = str(i).zfill(4)
print(change_i)
""" Output is :
0000
0001
0002
0003
...
0098
"""
for i in range(99):
change_i = str(i).zfill(5)
print(change_i)
""" Output is :
00000
00001
00002
00003
...
00098
"""

 

Source of problem :

I wanted to use it sorted(os.listdir(parh)) To read files in the order of their names , But because the name is not unified , So it didn't read as expected

 

Next, the numeric part of the file name is uniformly named as four digit

import os
path = "/data/cta/deal-with-dataset-b-v1/dicom_hessian"
for file in os.listdir(path):
num = file.split(".")[0].split("_")[-1]
filename_change = "dicom_hessian_" + num.zfill(4) + ".jpg"
os.rename(os.path.join(path, file), os.path.join(path, filename_change))

 

So far, the problem has been solved ~


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved