批量重命名文件删除其中一些
Mass-renaming files remove some of them
使用 os.rename(src, dest)
重命名许多文件时,我的一些文件消失了。
我在 Ubuntu.
In [66]: len(os.listdir())
Out[66]: 1430
In [67]: for i, name in enumerate(os.listdir()):
...: extension = os.path.splitext(name)[1]
...: dest = f"{i+1}{extension}"
...: os.rename(name, dest)
...:
In [68]: len(os.listdir())
Out[68]: 839
According to the FineManual(TM)(重点是我的):
os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission
很明显,如果您已经有匹配 f"{i+1}{extension}"
的文件,它们将被覆盖。
使用 os.rename(src, dest)
重命名许多文件时,我的一些文件消失了。
我在 Ubuntu.
In [66]: len(os.listdir())
Out[66]: 1430
In [67]: for i, name in enumerate(os.listdir()):
...: extension = os.path.splitext(name)[1]
...: dest = f"{i+1}{extension}"
...: os.rename(name, dest)
...:
In [68]: len(os.listdir())
Out[68]: 839
According to the FineManual(TM)(重点是我的):
os.rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None)
Rename the file or directory src to dst. If dst is a directory, OSError will be raised. On Unix, if dst exists and is a file, it will be replaced silently if the user has permission
很明显,如果您已经有匹配 f"{i+1}{extension}"
的文件,它们将被覆盖。