Home  >  Article  >  Backend Development  >  How to modify the folder name in Python

How to modify the folder name in Python

angryTom
angryTomOriginal
2020-02-28 13:19:0612809browse

How to modify the folder name in Python

How to modify the folder name in Python

Use the os.rename() method to modify the folder name in Python.

Recommended learning: Python video tutorial

Syntax:

os.rename(src, dst)

Parameters:

src -- the directory name to be modified

dst -- the modified directory name

Usage example:

#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os, sys
# 列出目录
print "目录为: %s"%os.listdir(os.getcwd())
# 重命名
os.rename("test","test2")
print "重命名成功。"
# 列出重命名后的目录
print "目录为: %s" %os.listdir(os.getcwd())

The output result of executing the above program is:

目录为:
[  'a1.txt','resume.doc','a3.py','test' ]
重命名成功。
[  'a1.txt','resume.doc','a3.py','test2' ]

PHP Chinese website, a large number of Introduction to Programming tutorials, welcome to learn!

The above is the detailed content of How to modify the folder name in Python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn