-2

I have a folder(eg. Folder 1) with files of ".tbw" extension and I wanted to create a new folder(eg. Folder 2) which contains the same files as of previous folder(Folder 1) but I want that the files that are copied from old folder to new folder should be changed with their extension i.e., all the files in the new folder(Folder 2) should have changed their extension to ".xml". And I want to automate this process using Python or any other programming/scripting language.

Basically I want to change the extension of files from ".tbw" to ".xml" using Python along with those I mentioned above.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Atul Agrawal
  • 105
  • 1
  • 2
  • 12
  • 1
    There are _tons_ of questiosn here regarding searching directories, copying files, renaming files. What did you do to solve it, what did you research, what went wrong? Where is your [mre] and what is your specific problem? – Patrick Artner Jun 25 '20 at 13:31
  • 1
    https://stackoverflow.com/questions/123198/how-do-i-copy-a-file-in-python?rq=1 – Patrick Artner Jun 25 '20 at 13:34
  • 1
    https://stackoverflow.com/questions/2491222/how-to-rename-a-file-using-python – Patrick Artner Jun 25 '20 at 13:34

1 Answers1

1

This should do the job:

import os

# Get a list of all files in a directory
files = os.listdir()

# Change file extensions
for file in files:
    base = os.path.splitext(file)[0]
    os.rename(file, base + ".xml")
JaniniRami
  • 493
  • 3
  • 11