background
It can move files or directories.
Print:
import shutil help() # Print as follows:''' move(src, dst, copy_function=<function copy2 at 0x000001D1CE15F8C8>) Recursively move a file or directory to another location. This is similar to the Unix "mv" command. Return the file or directory's destination. If the destination is a directory or a symlink to a directory, the source is moved inside the directory. The destination path must not already exist. If the destination already exists but is not a directory, it may be overwritten depending on () semantics. If the destination is on our current filesystem, then rename() is used. Otherwise, src is copied to the destination and then removed. Symlinks are recreated under the new name if () fails because of cross filesystem renames. The optional `copy_function` argument is a callable that will be used to copy the source or it will be delegated to `copytree`. By default, copy2() is used, but any function that supports the same signature (like copy()) can be used. A lot more could be done here... A look at a shows a lot of the issues this implementation glosses over. '''
View the function:
def move(src, dst, copy_function=copy2): """Recursively move a file or directory to another location. This is similar to the Unix "mv" command. Return the file or directory's destination. If the destination is a directory or a symlink to a directory, the source is moved inside the directory. The destination path must not already exist. If the destination already exists but is not a directory, it may be overwritten depending on () semantics. If the destination is on our current filesystem, then rename() is used. Otherwise, src is copied to the destination and then removed. Symlinks are recreated under the new name if () fails because of cross filesystem renames. The optional `copy_function` argument is a callable that will be used to copy the source or it will be delegated to `copytree`. By default, copy2() is used, but any function that supports the same signature (like copy()) can be used. A lot more could be done here... A look at a shows a lot of the issues this implementation glosses over. """ real_dst = dst if (dst): if _samefile(src, dst): # We might be on a case insensitive filesystem, # perform the rename anyway. (src, dst) return real_dst = (dst, _basename(src)) if (real_dst): raise Error("Destination path '%s' already exists" % real_dst) try: (src, real_dst) except OSError: if (src): linkto = (src) (linkto, real_dst) (src) elif (src): if _destinsrc(src, dst): raise Error("Cannot move a directory '%s' into itself" " '%s'." % (src, dst)) copytree(src, real_dst, copy_function=copy_function, symlinks=True) rmtree(src) else: copy_function(src, real_dst) (src) return real_dst
Mobile Directory
(old,new) used to move: folder:
old | It's a directory |
---|---|
new | It is an existing directory, and the old directory will be moved to the new; either new or non-existent directory will be created. At this time, all files under the old directory will be moved to the created directory. |
For example:
import shutil # Mobile Directory("./folder_123","./folder_456")
./folder_123:
--------------------------------------------------------------------------------------------------------------------------------
./folder_456:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
------------------------When the directory exists, move the folder_123 folder to the folder_456 folder;
Move files
(old,new) used to move: file:
old | is a file path |
---|---|
new | new is an existing folder path or an existing folder path plus file name |
Notice:
- If new is a folder path that does not exist, the original file will be moved to a directory on the new folder and renamed with the name of the folder.
- If new is a folder path that does not exist and adds a file name, an error will be reported.
For example:
import shutil # Move files("./mask/","./folder_456/folder_789")
./mask/:
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
./folder_456/folder_789:
-------------------- When the directory exists, move the ./mask/ file to the ./folder_456/folder_789 directory;
------------------------- When the directory does not exist, specific: folder_456 exists, and folder_789 does not exist, move ./mask/ to the folder_456 folder and rename the file to folder_789;
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.