
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
- Python Useful Resources
- Python - Questions and Answers
- Python - Quick Guide
- Python - Tools/Utilities
- Python - Useful Resources
- Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python os.rename() Method
Description
Python method rename() renames the file or directory src to dst.If dst is a file or directory(already present), OSError will be raised.
Syntax
Following is the syntax for rename() method −
os.rename(src, dst)
Parameters
src − This is the actual name of the file or directory.
dst − This is the new name of the file or directory.
Return Value
This method does not return any value.
Example
The following example shows the usage of rename() method.
# !/usr/bin/python import os, sys # listing directories print "The dir is: %s"%os.listdir(os.getcwd()) # renaming directory ''tutorialsdir" os.rename("tutorialsdir","tutorialsdirectory") print "Successfully renamed." # listing directories after renaming "tutorialsdir" print "the dir is: %s" %os.listdir(os.getcwd())\
When we run above program, it produces following result −
The dir is: [ 'a1.txt','resume.doc','a3.py','tutorialsdir','amrood.admin' ] Successfully renamed. The dir is: [ 'a1.txt','resume.doc','a3.py','tutorialsdirectory','amrood.admin' ]
python_files_io.htm
Advertisements