Remove specific characters from the filename using Python.

huangapple go评论57阅读模式
英文:

remove specific characters from the filename using Python

问题

Sure, I'll provide the translation:

如何通过仅删除下划线字符来从文件夹中重命名多个文件?

例如:1_2_3.txt
改为 123.txt

英文:

how to rename multiple files from a folder by deleting only the underline character?

Ex: 1_2_3.txt
To 123.txt

答案1

得分: 3

使用Python的os

folder_path = "插入您的文件夹路径"
for filename in os.listdir(folder_path):
    if "_" in filename:
        new_filename = filename.replace("_", "")
        os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_filename))
英文:

Use Python's os package

folder_path = "INSERT PATH TO YOUR FOLDER HERE"
for filename in os.listdir(folder_path):
    if "_" in filename:
        new_filename = filename.replace("_", "")
        os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, new_filename))

huangapple
  • 本文由 发表于 2023年4月11日 05:40:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75980945.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定