Python provides functionality to move files or directories from one location to another location. This can be achieved using
shutil.move() function from shutil module.
shutil.move() method Recursively moves a file or directory (source) to another location (destination) and returns the destination. If the destination directory already exists then src is moved inside that directory. If the destination already exists but is not a directory then it may be overwritten depending on
Syntax: shutil.move(source, destination, copy_function = copy2)
source: A string representing the path of the source file.
destination: A string representing the path of the destination directory.
copy_function (optional): The default value of this parameter is copy2. We can use other copy functions like copy, copytree, etc for this parameter.
Return Value: This method returns a string that represents the path of a newly created file.
We want to move directory B into directory A. Below is the implementation.
Example #2: Now let’s suppose we want to move all the sub-directories and files of above directory A to directory G using
shutil.copytree() and the destination directory does not exist. Below is the implementation.
- Python | Move or Copy Files and Directories
- Listing out directories and files in Python
- Python | Arrange the files in directories according to extensions
- Python: Get List of all empty Directories
- Interact with files in Python
- Working with PDF files in Python
- HDF5 files in Python
- Writing CSV files in Python
- Downloading files from web using Python
- Working with zip files in Python
- Working with csv files in Python
- Reading CSV files in Python
- Python | Reading .ini Configuration Files
- Python | Merge two text files
- Rename multiple files using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.