python中os.replace()函数的具体过程

首先创建了两个文件:
1.txt 内容是1
2.txt 内容是2

import os
os.replace('1.txt','2.txt')

执行后发现只剩下一个: 2.txt
但内容是1.

所以猜测
os.replace(file1,file2), 这个函数相当于用file2file1 重命名,并删除file2

import os
os.remove('2.txt')
os.rename('1.txt', '2.txt')

功能相同

另附上python中的解释:
Rename a file or directory, overwriting the destination.

If either src_dir_fd or dst_dir_fd is not None, it should be a file
descriptor open to a directory, and the respective path string (src or dst)
should be relative; the path will then be relative to that directory.
src_dir_fd and dst_dir_fd, may not be implemented on your platform.
If they are unavailable, using them will raise a NotImplementedError."

翻译过来:

重命名文件或目录,覆盖目标。

如果src_dir_fd或dst_dir_fd不是None,那么它应该是一个文件
打开到一个目录的描述符,以及相应的路径字符串(src或dst)
应该是相对的;然后,路径将相对于该目录。
src_dir_fd和dst_dir_fd可能无法在您的平台上实现。
如果它们不可用,则使用它们将引发NotImplementedError。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值