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,790 points
20,016 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. 

Hope this helps!

If you need to know more about Python, join Python online course certification today.

Thanks!

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

shutil.move('a.txt', 'b.kml')
answered Oct 12, 2018 by findingbugs
• 4,780 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 15, 2019 by Shashank
• 1,370 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,440 points
1,308 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,560 points
10,477 views
0 votes
2 answers

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

import os from optparse import OptionParser, Option class MyOption ...READ MORE

answered Jul 29, 2020 in Python by The real slim shady
4,724 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, 2019 in Python by Nymeria
• 3,560 points
1,134 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, 2019 in Python by Waseem
• 4,540 points
585 views
0 votes
2 answers
+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 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,442 views
0 votes
1 answer
+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
• 69,220 points
2,126 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP