How do I rename a file?
How can I renam开发者_如何学Pythone a file on unix platform programatically without using the standard rename function?
The historical way to rename a file is to use link(2) to create a new hardlink to the same file, then to use unlink(2) to remove the old name.
The following is a somewhat ironic solution, that does not use the standard rename(2)
system call by itself:
#include <stdlib.h>
if (system("mv file1 file2") != 0)
perror("system");
It's an indirect usage of rename(2)
, this syscall is invoked by mv(1)
.
精彩评论