开发者

Moving a file to another directory in Perl?

开发者 https://www.devze.com 2023-03-16 10:57 出处:网络
rename $ARGV[0], $ARGV[1] or warn \"rename fails: $!\\n\"; When I 开发者_如何学Pythonrun ./programe file.txt dir/ it always fails.
rename $ARGV[0], $ARGV[1] or warn "rename fails: $!\n";

When I 开发者_如何学Pythonrun ./programe file.txt dir/ it always fails.

What's wrong here?


From the perldoc for rename:

Behavior of this function varies wildly depending on your system implementation. For example, it will usually not work across file system boundaries, even though the system mv command sometimes compensates for this. Other restrictions include whether it works on directories, open files, or pre- existing files. Check perlport and either the rename(2) manpage or equivalent system documentation for details.

For a platform independent "move" function look at the File::Copy module.

So use File::Copy::move instead.


rename is expecting a full target path, not just a directory. Try:

./progname file.txt dir/file.txt

It would be nice if you would tell what error it failed with...


rename changes the name of a file, which is not what you're trying to do. You probably want to use the move function from File::Copy

0

精彩评论

暂无评论...
验证码 换一张
取 消