python os renames_Python os.renames()用法及代码示例

Python中的OS模块提供了与操作系统进行交互的功能。操作系统属于Python的标准实用程序模块。该模块提供了使用依赖于操作系统的功能的便携式方法。

os.renames()方法是递归目录或文件重命名功能。它像os.rename()方法,除了创建所需的任何中间目录外,然后首先尝试进行该操作。重命名完成后,将使用以下命令删除与旧名称的最右边路径段相对应的目录os.removedirs()。

用法: os.renames(old, new)

参数:

old:这是要重命名的文件或目录的旧名称。

new:这是文件或目录的新名称。它可以将文件包含到

目录或不存在的整个目录树。

Note:它还可以接受旧的和新的path-like对象。

返回值:此方法不返回任何值。

范例1:使用os.renames()重命名文件的方法

# Python program to explain os.renames() method

# importing os module

import os

# path

path = 'C:/Users/Rajnish/Desktop/GeeksforGeeks'

# Changing directory

os.chdir(path)

# Printing current directory

print ("Current directory is:", os.getcwd())

# List files and directories

# in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'

print("Before renaming file:")

print(os.listdir(os.getcwd()))

# Rename the file

# Using os.renames() method

os.renames('testfile.txt', 'new_name.txt')

# List files and directories

# in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'

print("After renaming file:")

print(os.listdir(os.getcwd()))

输出:

Current directory is:C:\Users\Rajnish\Desktop\GeeksforGeeks

Before renaming file:

['testfile.txt']

After renaming file:

['new_name.txt']

范例2:

使用os.renames()重命名文件并将其添加到不存在的新目录中的方法

# Python program to explain os.renames() method

# importing os module

import os

# path

path = 'C:/Users/Rajnish/Desktop/GeeksforGeeks'

# Changing directory

os.chdir(path)

# Printing current directory

print ("Current directory is:" os.getcwd())

# List files and directories

# in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'

print("Before renaming file:")

print(os.listdir(os.getcwd()))

# Rename the file and

# adding the file in new

# directory name 'newdir'

# Using os.renames() method

os.renames('testfile.txt', 'newdir / new_name.txt')

# List files and directories

# in 'C:/Users/Rajnish/Desktop/GeeksforGeeks'

print("After renaming file:")

print(os.listdir(os.getcwd()))

输出:

Current directory is:C:\Users\Rajnish\Desktop\GeeksforGeeks

Before renaming file:

['newdir', 'testfile.txt']

After renaming file:

['newdir']

ERROR: Exception: Traceback (most recent call last): File "c:\program files\python\lib\shutil.py", line 788, in move os.rename(src, real_dst) PermissionError: [WinError 5] 拒绝访问。: 'c:\\program files\\python\\lib\\site-packages\\flask-1.1.2.dist-info\\' -> 'C:\\Users\\弦引\\AppData\\Local\\Temp\\pip-uninstall-0g0yzsps' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\弦引\AppData\Roaming\Python\Python38\site-packages\pip\_internal\cli\base_command.py", line 180, in exc_logging_wrapper status = run_func(*args) File "C:\Users\弦引\AppData\Roaming\Python\Python38\site-packages\pip\_internal\commands\uninstall.py", line 105, in run uninstall_pathset = req.uninstall( File "C:\Users\弦引\AppData\Roaming\Python\Python38\site-packages\pip\_internal\req\req_install.py", line 687, in uninstall uninstalled_pathset.remove(auto_confirm, verbose) File "C:\Users\弦引\AppData\Roaming\Python\Python38\site-packages\pip\_internal\req\req_uninstall.py", line 381, in remove moved.stash(path) File "C:\Users\弦引\AppData\Roaming\Python\Python38\site-packages\pip\_internal\req\req_uninstall.py", line 272, in stash renames(path, new_path) File "C:\Users\弦引\AppData\Roaming\Python\Python38\site-packages\pip\_internal\utils\misc.py", line 318, in renames shutil.move(old, new) File "c:\program files\python\lib\shutil.py", line 800, in move rmtree(src) File "c:\program files\python\lib\shutil.py", line 737, in rmtree return _rmtree_unsafe(path, onerror) File "c:\program files\python\lib\shutil.py", line 615, in _rmtree_unsafe onerror(os.unlink, fullname, sys.exc_info()) File "c:\program files\python\lib\shutil.py", line 613, in _rmtree_unsafe os.unlink(fullname) PermissionError: [WinError 5] 拒绝访问。: 'c:\\program files\\python\\lib\\site-packages\\flask-1.1.2.dist-info\\entry_points.txt'
07-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值