使用 python - regex 重命名文件
我想使用 python 重命名 1k 文件。它们基本上都是 somejunkDATE.doc 格式
,我想删除所有垃圾,只留下日期。我不确定如何将其与目录中的所有文件相匹配。
谢谢
I am wanting to rename 1k files using python. they are all in the format somejunkDATE.doc
basically, I would like to delete all the junk, and only leave the date. I am unsure how to match this for all files in a directory.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您的日期格式始终相同,则只需使用切片
如果您想检查数字是否为有效日期,请将
file[-12:-3]
传递给time
或要检查的datetime
模块。假设你的文件都在一个目录中(没有子目录)
If your date format is the same throughout, just use slicing
If you want to check if the numbers are valid dates, pass
file[-12:-3]
totime
ordatetime
module to check.Say your files are all in a directory (no sub directories)