Change the file extension for files in a folder?

后端 未结 5 1713
逝去的感伤
逝去的感伤 2020-12-05 00:31

I would like to change the extension of the files in specific folder. i read about this topic in the forum. using does ideas, I have written following code and I expect that

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-05 01:30

    Something like this will rename all files in the executing directory that end in .txt to .text

    import os, sys
    
    for filename in os.listdir(os.path.dirname(os.path.abspath(__file__))):
      base_file, ext = os.path.splitext(filename)
      if ext == ".txt":
        os.rename(filename, base_file + ".text")
    

提交回复
热议问题