English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Python implementation of the method to batch change the file extension of files in a specified directory

This article describes a method to batch change the file extension of files in a specified directory using Python. Share it with everyone for reference, as follows:

# Encoding=utf-8
# Author: walker
# Date: 2013-12-06
# Function: Deeply traverse specified directory and change specified extension
import os
import os.path
# Read in specified directory and convert to absolute path
rootdir = raw_input('root dir:\n')
rootdir = os.path.abspath(rootdir)
print('absolute path:\n' + rootdir)
# Read in original extension and standardize
old_ext = raw_input('old extension:\n')
old_ext = old_ext.strip()
if old_ext[0] != '.':
  old_ext = '.' + old_ext
# Read in new extension and standardize
new_ext = raw_input('new extension:\n')
new_ext = new_ext.strip()
if new_ext[0] != '.':
  new_ext = '.' + new_ext
for parent, dirnames, filenames in os.walk(rootdir):
  for filename in filenames:
    pathfile = os.path.join(parent, filename)
    if pathfile.endswith(old_ext):
      new_pathfile = os.path.splitext(pathfile)[0] + new_ext
      print('=======================================================')
      print(pathfile)
      print('-------------------------------------------------------
      print(new_pathfile)
      print('=======================================================')
      os.rename(pathfile, new_pathfile)

PS:The above function can also be implemented with one shell command

# Replace the suffix .ini with .txt
# The path name can be a relative path or an absolute path
find path name | rename 's/\.ini$/\.txt/'

Note that the rename command above is the Perl version of the rename command.

PS2:scandir compatible code.

# Use the built-in version of scandir/walk if possible, otherwise
# use the scandir module version
try:
  from os import scandir, walk  #python3.5+
except ImportError:
  from scandir import scandir, walk #python3.4-

Readers who are interested in more content related to Python can check the special topics on this site: 'Summary of Python File and Directory Operation Skills', 'Summary of Python Text File Operation Skills', 'Summary of Python URL Operation Skills', 'Summary of Python Image Operation Skills', 'Python Data Structures and Algorithms Tutorial', 'Summary of Python Socket Programming Skills', 'Summary of Python Function Usage Skills', 'Summary of Python String Operation Skills', and 'Classic Tutorial of Python入门与进阶'.

I hope the content described in this article will be helpful to everyone in Python program design.

Declaration: The content of this article is from the network, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been edited by humans, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like