1

How to change the file name prefix in a directory. I need to replace the white space in the file name prefix"Mohammed " to "Mohammed".

Directory:

C:\Mohammad_eng\Mohammad _eng-1.txt
C:\Mohammad_eng\Mohammad _eng-1.wav
C:\Mohammad_eng\Mohammad _eng-2.txt
C:\Mohammad_eng\Mohammad _eng-2.wav
C:\Mohammad_eng\Mohammad _eng-3.txt
C:\Mohammad_eng\Mohammad _eng-3.wav
C:\Mohammad_eng\Mohammad _eng-4.txt
C:\Mohammad_eng\Mohammad _eng-4.wav
C:\Mohammad_eng\Mohammad _eng-5.txt
C:\Mohammad_eng\Mohammad _eng-5.wav
C:\Mohammad_eng\Mohammad _eng-6.txt
C:\Mohammad_eng\Mohammad _eng-6.wav
C:\Mohammad_eng\Mohammad _eng-7.txt
C:\Mohammad_eng\Mohammad _eng-7.wav
C:\Mohammad_eng\Mohammad _eng-8.txt
C:\Mohammad_eng\Mohammad _eng-8.wav
C:\Mohammad_eng\Mohammad _eng-9.txt
C:\Mohammad_eng\Mohammad _eng-9.wav
C:\Mohammad_eng\Mohammad _eng-10.txt
C:\Mohammad_eng\Mohammad _eng-10.wav

I need to change them to

C:\Mohammad_eng\Mohammad_eng-1.txt
C:\Mohammad_eng\Mohammad_eng-1.wav
C:\Mohammad_eng\Mohammad_eng-2.txt
C:\Mohammad_eng\Mohammad_eng-2.wav
C:\Mohammad_eng\Mohammad_eng-3.txt
C:\Mohammad_eng\Mohammad_eng-3.wav
C:\Mohammad_eng\Mohammad_eng-4.txt
C:\Mohammad_eng\Mohammad_eng-4.wav
C:\Mohammad_eng\Mohammad_eng-5.txt
C:\Mohammad_eng\Mohammad_eng-5.wav
C:\Mohammad_eng\Mohammad_eng-6.txt
C:\Mohammad_eng\Mohammad_eng-6.wav
C:\Mohammad_eng\Mohammad_eng-7.txt
C:\Mohammad_eng\Mohammad_eng-7.wav
C:\Mohammad_eng\Mohammad_eng-8.txt
C:\Mohammad_eng\Mohammad_eng-8.wav
C:\Mohammad_eng\Mohammad_eng-9.txt
C:\Mohammad_eng\Mohammad_eng-9.wav
C:\Mohammad_eng\Mohammad_eng-10.txt
C:\Mohammad_eng\Mohammad_eng-10.wav

How to achieve that? Note: I am new in python scripting.

Whatman
  • 104
  • 6

2 Answers2

3

Please use the following, tested and verified, this will work.

import glob, os


print("File rename Sample- rename()")


def replace(dir, pattern, titlePattern, replaceFromStr, replaceToStr):
    for pathAndFilename in glob.iglob(os.path.join(dir, pattern)):
        title, ext = os.path.splitext(os.path.basename(pathAndFilename))
        os.rename(pathAndFilename,
                  os.path.join(dir, titlePattern % title.replace(replaceFromStr, replaceToStr) + ext))


replace(r'C:\Mohammad_eng', r'*.wav', r'%s', "Mohammad ", "Mohammad")
replace(r'C:\Mohammad_eng', r'*.txt', r'%s', "Mohammad ", "Mohammad")
Hardian
  • 1,922
  • 22
  • 23
  • Checked this solution, it is a great solution with "glob module that finds all the pathnames matching", thank you for the support. – Whatman Apr 23 '19 at 18:53
1

See How to rename a file using Python

# call this script from the respective folder
import os
import re

for filename in os.listdir('.'):
    match = re.match(r'Mohammad (.*)', filename)
    if match:
        new_filename = 'Mohammad' + match.group(1)
        os.rename(filename, new_filename)

This goes through all filenames and directory names and checks whether the name matches the specified pattern. If so, it will rename the file dropping the space.

Michael H.
  • 3,323
  • 2
  • 23
  • 31
  • Where do i specifiy the directory path ("C:\Mohammad_eng")? – Whatman Apr 23 '19 at 18:42
  • Like the comment says: Call the script from the folder `C:\Mohammad_eng`, i.e. in PowerShell type `cd C:\Mohammad_eng`, store this script as `script.py` in `C:\Mohammad_eng` and then type `python script.py` in PowerShell. – Michael H. Apr 23 '19 at 18:47