How can I rename a file in Python?

+3 votes
I am unable to rename my file using "refractor" option. Can it be done by writing a script or is there any other method to rename a file?
Mar 31, 2018 in Python by hemant
• 5,750 points
129 views

7 answers to this question.

+2 votes

yes, you can use "os.rename" for that. Refer the below code:

import os

os.rename("a.py","test.py")

Here, a.py file has been renamed to "test.py" file. 

answered Mar 31, 2018 by DareDev
• 6,810 points
+1 vote
import shutil

shutil.move('a.txt', 'b.kml')
answered Oct 12, 2018 by findingbugs
• 4,750 points
+1 vote


  1. import os
  2.  
  3. path = input("Enter the directory path where you need to rename: ")
  4. for filename in os.listdir(path):
  5. filename_without_ext = os.path.splitext(filename)[0]
  6. extension = os.path.splitext(filename)[1]
  7. new_file_name = filename_without_ext+"_n"
  8. new_file_name_with_ext = new_file_name+extension
  9. print(new_file_name_with_ext)
  10. os.rename(os.path.join(path,filename),os.path.join(path,new_file_name_with_ext))

My Sample output :

image

It worked for me.Test run the script to see if it works for you also. You can get back to me if any step is incorrect.

Happy to help.

answered Oct 12, 2018 by abc
+1 vote

Here's a script based on your newest comment.

#!/usr/bin/env python
from os import rename, listdir

badprefix = "cheese_"
fnames = listdir('.')

for fname in fnames:
    if fname.startswith(badprefix*2):
        rename(fname, fname.replace(badprefix, '', 1))
answered Oct 12, 2018 by Riya
+1 vote

Assuming you are already in the directory, and that the "first 8 characters" from your comment hold true always. (Although "CHEESE_" is 7 characters... ? If so, change the 8 below to 7)

from glob import glob
from os import rename
for fname in glob('*.prj'):
    rename(fname, fname[8:])
answered Oct 12, 2018 by rani
+1 vote

I have the same issue, where I want to replace the white space  in any pdf file to a dash -. But the files were in multiple sub-directories. So, I had to use os.walk(). In your case for multiple sub-directories, it could be something like this:

import os
for dpath, dnames, fnames in os.walk('/path/to/directory'):
    for f in fnames:
        os.chdir(dpath)
        if f.startswith('cheese_'):
            os.rename(f, f.replace('cheese_', ''))
answered Oct 12, 2018 by Kalpesh
0 votes
You can use the os module and rename the file.
Import os
os.rename("old file","new file")
answered Feb 14 by Shashank
• 1,350 points

Related Questions In Python

+2 votes
2 answers

How can I create a new file in Python?

You can try the below code which ...READ MORE

answered Mar 31, 2018 in Python by anto.trigg4
• 3,420 points
91 views
0 votes
1 answer

How can I parse a YAML file in Python?

Read & Write YAML files with Python ...READ MORE

answered Nov 21, 2018 in Python by Nymeria
• 3,520 points
1,790 views
0 votes
1 answer

How can I rename multiple files in a certain directory using Python?

Use os.rename(src, dst) to rename or move a file ...READ MORE

answered Nov 23, 2018 in Python by ariaholic
• 7,340 points
118 views
0 votes
1 answer

How can I read numbers in Python from a custom file?

Hi, good question. Let us first assume that ...READ MORE

answered Feb 6 in Python by Nymeria
• 3,520 points
71 views
0 votes
0 answers

how can i read a text file in python?

can you specify the syntax and prequisites ...READ MORE

Apr 4 in Python by Waseem
• 4,480 points
25 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 226 views
+4 votes
8 answers

How can I use Python's range function?

The range function is mostly used in for-loop.  Ex: for ...READ MORE

answered Aug 21, 2018 in Python by Omkar
• 67,460 points
53 views