首先创建了两个文件:
1.txt 内容是1
2.txt 内容是2
import os
os.replace('1.txt','2.txt')
执行后发现只剩下一个: 2.txt
但内容是1.
所以猜测
os.replace(file1,file2), 这个函数相当于用file2 给 file1 重命名,并删除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。