implementing mv

H

Hong Jiang

Hi,

Is there an efficient way to implement the 'mv' command? I mean not by
calling external commands.
Moving a file can be done by first making a hard link then unlinking the
original file. But I cannot figure out a good way to move a directory.
Do I have to move all the files and subdirectories recursively?

Thanks.
 
J

Jeff Epler

You should look at the 'shutil' module. It provides a move() function.

$ pydoc shutil.move
Help on function move in shutil:

shutil.move = move(src, dst)
Recursively move a file or directory to another location.

If the destination is on our current filesystem, then simply use
rename. Otherwise, copy src to the dst and then remove src.
A lot more could be done here... A look at a mv.c shows a lot of
the issues this implementation glosses over.
(I assume mv.c is the source for the unix "/bin/mv" utility, available
in multiple incarnations)

Jeff
 
D

David M. Wilson

Martin v. Loewis said:
Try shutil.move.

'Sometimes' os.rename can do moves too, however for your purposes
shutil.move sounds better suited.


David.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top