A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/python/os_renames.htm below:

Python os.renames() Method

Python os.renames() Method

Python method renames() of OS module is a recursive directory or file renaming function. It does the same functioning as os.rename(), but it also creates intermediate directories if they do not exist to complete the new file path.

Syntax

Following is the syntax for Python os.renames() method −

os.renames(old, new)
Parameters

The parameters of Python os.renames() method is shown below −

Return Value

The Python os.renames() method does not return any value.

Example

The following example shows the usage of renames() method. Here, we are renaming "aa1.txt" file to "aanew.txt" and moving it to a new directory named "newdir".

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()))

When we run above program, it produces 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 not visible here, as it has been moved to newdir and renamed as aanew.txt. The directory "newdir" and its contents are shown below −

[ 'aanew.txt' ]

python_files_io.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4