Sunday, August 16, 2020

Python os.renames() Method with example



Hello dear readers! Welcome back to another section of my tutorial on Python. In this tutorial guide, we are going to be studying about the Os renames() method.

The Python os renames() method is a repetitive directory or file renaming function. It does same functioning as os.rename(), but it also moves a file to a directory, or a whole tree of directories, that do not exist.

Syntax

The following below is the syntax for Python Os renames() method -

os.renames(old,  new)


Parameter Details

  • old - This is the real name of the file or directory to be renamed.
  • new - This is the new name of the file or directory. It can include a file to a directory, or a whole tree of directories, that do not exist.

Return Value

It does not return any value.

Example

The following below is a simple example -

# !/usr/bin/python

import os, sys
print "Current directory is: %s" %os.getcwd()

# listing directories
print "The dir is: %s"%os.listdir(os.getcwd())

# renaming file "aa1.txt"
os.renames("aa1.txt","newdir/aanew.txt")

print "Successfully renamed."

# listing directories after renaming and moving "aa1.txt"
print "The dir is: %s" %os.listdir(os.getcwd())

Output
When the above code is executed, it will produce the following result -

Current directory is: /tmp
The dir is:
 [  'a1.txt','resume.doc','a3.py','aa1.txt','Administrator','amrood.admin' ]
Successfully renamed.
The dir is:
 [  'a1.txt','resume.doc','a3.py','Administrator','amrood.admin' ]

The file aa1.txt is visible here, as it's been moved to newdir and renamed as aanew.txt. The directory newdir and it's contents are shown below -

[ 'aanew.txt' ]


Alright guys! This is where we are rounding up for this tutorial post. In my next tutorial, we are going to be discussing about the Python OS rmdir() Method.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.