Open In App

How to move Files and Directories in Python

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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 os.rename() semantics.

Syntax: shutil.move(source, destination, copy_function = copy2)

Parameters:
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.

Example #1: Suppose the structure of directory looks like this –

Python-list-of-directories

Inside Test:

python-move-files-and-dir

Inside A:

python-move-files-and-dir

We want to move directory B into directory A. Below is the implementation.




# Python program to move
# files and directories
  
  
import shutil
  
# Source path
source = "D:\Pycharm projects\gfg\Test\B"
  
# Destination path
destination = "D:\Pycharm projects\gfg\Test\A"
  
# Move the content of
# source to destination
dest = shutil.move(source, destination)
  
# print(dest) prints the 
# Destination of moved directory


Output:

Inside test:

python-move-files-and-dir

Inside A:

python-move-files-and-dir

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 program to move
# files and directories
  
  
import shutil
  
# Source path
source = "D:\Pycharm projects\gfg\Test\A"
  
# Destination path
destination = "D:\Pycharm projects\gfg\Test\G"
  
# Move the content of
# source to destination
dest = shutil.move(source, destination, copy_function = shutil.copytree)
  
# print(dest) prints the
# Destination of moved directory


Output:

Inside Test:

python-move-files-and-dir

Inside G:

python-move-files-and-dir



Previous Article
Next Article

Similar Reads

Python | Move or Copy Files and Directories
Let's say we want to copy or move files and directories around, but don’t want to do it by calling out to shell commands. The shutil module has portable implementations of functions for copying files and directories. Code #1 : Using shutil module import shutil # Copy src to dst. (cp src dst) shutil.copy(src, dst) # Copy files, but preserve metadata
3 min read
Python - Move Files To Creation and Modification Date Named Directories
We all understand how crucial it is to manage files based on their creation and modification dates. So, in this article, we will try to build a Python script that would move all of your files to new directories based on their creation and modification dates. Basically, it will look for directories and, if any are found, it will extract all the file
4 min read
How to List all Files and Directories in FTP Server using Python?
FTP ( File Transfer Protocol ) is set of rules that computer follows to transfer files across computer network. It is TCP/IP based protocol. FTP lets clients share files. FTP is less secure because of files are shared as plain text without any encryption across the network. It is possible using python to retrieve list of file and directories from F
2 min read
Listing out directories and files in Python
The following is a list of some of the important methods/functions in Python with descriptions that you should know to understand this article. len() - It is used to count number of elements(items/characters) of iterables like list, tuple, string, dictionary etc. str() - It is used to transform data value(integers, floats, list) into string. abspat
6 min read
Python | Arrange the files in directories according to extensions
In this article, we will learn how to arrange the files in different directories according to their extensions. When you have a lot of files with different extension than you can simply use the script for your help. Below is the Python implementation - import os # Using system functions import shutil # Moving file to different folders. # Changing t
2 min read
Create temporary files and directories using tempfile
Python tempfile module allows you to create a temporary file and perform various operations on it. Temporary files may be required when we need to store data temporarily during the program's execution or when we are working with a large amount of data. These files are created with unique names and stored in a platform-dependent default location. Th
5 min read
Python - Move and overwrite files and folders
In this article, we will be learning on moving a collection of files and folders where there may be files/folders with the same name as in the source name in the destination. So that we may need to overwrite the existing destination file with the source file. The shutil.move() method is used to move a file or directory from one place to another. If
3 min read
How to move all files from one directory to another using Python ?
In this article, we will see how to move all files from one directory to another directory using Python. In our day-to-day computer usage we generally copy or move files from one folder to other, now let's see how to move a file in Python: This can be done in two ways:Using os module.Using shutil module.Source and Destination Folder Setup Before Ru
2 min read
Python - Move all files from subfolders to main folder
This article will discuss how to move all files from the subfolder to the main folder using Python. The approach is simple it is similar to moving files from one folder to another using Python, except here the main folder or parent folder of the subfolder is passed as the destination. Modules UsedOS Module: OS module in Python provides functions to
3 min read
Python: Get List of all empty Directories
The OS module in Python is used for interacting with the operating system. This module comes with Python's standard utility module so there is no need to install it externally. All functions in the OS module raise OSError in the case of invalid or inaccessible file names and paths, or other arguments that have the correct type but are not accepted
2 min read
three90RightbarBannerImg