Skip to content

Instantly share code, notes, and snippets.

@zackbraksa
Last active November 19, 2021 20:25
Show Gist options
  • Save zackbraksa/2e84f450686ea5e18c11 to your computer and use it in GitHub Desktop.
Save zackbraksa/2e84f450686ea5e18c11 to your computer and use it in GitHub Desktop.
Change the filename extension of multiple files in a directory.( e.g change all .html to .php )

Extensions

Change the filename extension of multiple files in a directory.( e.g change all .html to .php )

Usage

  1. Place "extensions.py" file inside the directory where you want to change file extensions.

  2. Open your terminal, execute a command that should look like this :

     python extensions.py oldExtension newExtension
    

Where oldExtension should be the file extension you wanna change (html,php ...) for all files in that directory. And newExtension is the extenstion you wanna change oldExtension to.

##Examples In your terminal :

python extensions.py html php 
import os
import sys
import glob
#author Braksa Zakaria
def change(old_ext,new_ext):
[ os.rename( f,"%s.%s" % (os.path.splitext(f)[0],new_ext) ) for f in glob.glob(os.getcwd() + "/*." + old_ext) ]
if __name__ == "__main__":
if len(sys.argv) > 2:
change(sys.argv[1],sys.argv[2])
@athulkannan2000
Copy link

Sir actually there is an exception will occur in most of the linux distribution systems, From my experience. I put the actual code here.
import os
import sys
import glob

#author Athul A

path = "File name"
def change(old_ext,new_ext):
[ os.rename( f,"%s.%s" % (os.path.splitext(f)[0],new_ext) ) for f in glob.iglob(path + "/**/*." + old_ext) ]

optional Section

if name == "main":
if len(sys.argv) > 2:
change(sys.argv[1],sys.argv[2])

OR you can directly put the following commands

change(old_ex,new_ex)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment