【问题标题】:Renaming files that have timestamp in python在python中重命名具有时间戳的文件
【发布时间】:2022-07-08 00:17:38
【问题描述】:

我有一个包含 1000 个具有这种模式的协调文件的文件夹:coord.*(* 是从 0 开始并每次增加 10000 的时间步(coord.0,coord.10000,...)。我想将它们全部重命名为 coordN.*(保留时间戳,仅添加字母 N)。关于我应该如何在 python 上执行此操作的任何建议?提前谢谢!

【问题讨论】:

标签: python rename


【解决方案1】:

您可以使用glob 获取路径中模式为coord.* 的所有文件,并将它们重命名为os.rename

import os
import glob

path = 'Files/'

for file in glob.glob(path+'coord.*'):
    f,s = file.split('.',1)
    file_new = f+'N.'+s
    os.rename(file, file_new)

第一个文件名:

coord.1.txt
coord.1.png

重命名后:

coordN.1.txt
coordN.1.png

【讨论】:

  • @PranavHosangadi,查看更新...
猜你喜欢
  • 2016-05-06
  • 2022-08-11
  • 1970-01-01
  • 2014-08-05
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-04
相关资源
最近更新 更多