Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename a file using Python

I want to change a.txt to b.kml.

like image 768
zjm1126 Avatar asked Mar 22 '10 09:03

zjm1126


People also ask

Which method is used to rename a file in Python?

rename() method in Python is used to rename a file or directory. This method renames a source file/ directory to specified destination file/directory.

How do I rename a python file in Terminal?

To rename a file in the terminal, move the file with mv from itself to itself with a new name. Here's an example. To rename a file on a computer with a graphical interface, you open a window, find the file you want to rename, click on its name (or right-click and select the option to rename), and then enter a new name.

How do I bulk rename files in Python?

rename() method is used to rename a file or directory in Python3. The rename() method is a part of the os module.


4 Answers

Use os.rename:

import os

os.rename('a.txt', 'b.kml')

Usage:

os.rename('from.extension.whatever','to.another.extension')
like image 126
YOU Avatar answered Oct 12 '22 08:10

YOU


File may be inside a directory, in that case specify the path:

import os
old_file = os.path.join("directory", "a.txt")
new_file = os.path.join("directory", "b.kml")
os.rename(old_file, new_file)
like image 39
Abdul Razak Avatar answered Oct 12 '22 06:10

Abdul Razak


As of Python 3.4 one can use the pathlib module to solve this.

If you happen to be on an older version, you can use the backported version found here

Let's assume you are not in the root path (just to add a bit of difficulty to it) you want to rename, and have to provide a full path, we can look at this:

some_path = 'a/b/c/the_file.extension'

So, you can take your path and create a Path object out of it:

from pathlib import Path
p = Path(some_path)

Just to provide some information around this object we have now, we can extract things out of it. For example, if for whatever reason we want to rename the file by modifying the filename from the_file to the_file_1, then we can get the filename part:

name_without_extension = p.stem

And still hold the extension in hand as well:

ext = p.suffix

We can perform our modification with a simple string manipulation:

Python 3.6 and greater make use of f-strings!

new_file_name = f"{name_without_extension}_1"

Otherwise:

new_file_name = "{}_{}".format(name_without_extension, 1)

And now we can perform our rename by calling the rename method on the path object we created and appending the ext to complete the proper rename structure we want:

p.rename(Path(p.parent, new_file_name + ext))

More shortly to showcase its simplicity:

Python 3.6+:

from pathlib import Path
p = Path(some_path)
p.rename(Path(p.parent, f"{p.stem}_1_{p.suffix}"))

Versions less than Python 3.6 use the string format method instead:

from pathlib import Path
p = Path(some_path)
p.rename(Path(p.parent, "{}_{}_{}".format(p.stem, 1, p.suffix))
like image 83
idjaw Avatar answered Oct 12 '22 08:10

idjaw


import shutil

shutil.move('a.txt', 'b.kml')

This will work to rename or move a file.

like image 68
Andy Balaam Avatar answered Oct 12 '22 07:10

Andy Balaam