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

How to modify the folder name in Python

angryTom
Release: 2020-02-28 13:19:06
Original
12839 people have browsed it

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)
Copy after login

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())
Copy after login

The output result of executing the above program is:

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template