The move method of the shutil module
The move method of the shutil module moves a file or folder. Folders are moved along with their contents.
The first parameter of the method specifies the path to the source file, the second parameter specifies the destination path of the new file, and the third optional parameter specifies the file copy function (by default copy_function=copy2).
The method returns the destination path of the new file.
If the original and new paths are the same, the method simply overwrites the contents of the specified file.
Syntax
import shutil
shutil.move(where do we copy from, where do we copy to, [copy function])
Example
Let's move the file file1.txt:
import shutil
print(shutil.move('file1.txt', 'file2.txt'))
Example
Let's move the folder dir with all its contents:
import shutil
print(shutil.move('dir', 'trg'))
See also
-
method
removeof moduleos,
which deletes the file -
method
copyof moduleshutil,
which copies a file while preserving the access mode -
method
getcwdmoduleos,
which returns the current working directory -
method
path.existsof moduleos,
which checks the existence of a path -
method
path.isdirof moduleos,
which checks the existence of a folder -
method
path.isfileof moduleos,
which checks the existence of a file -
method
scandirof moduleos,
which iterates through the files in a folder -
method
rmtreeof moduleshutil,
which recursively deletes a folder -
method
copy2of moduleshutil,
which copies a file with metadata