JIYIK CN >

Current Location:Home > Learning > PROGRAM > Python >

Move files from one directory to another using Python

Author:JIYIK Last Updated:2025/05/06 Views:

Moving files from one directory to another might not sound like a big deal, but sometimes, it helps a lot in manipulating files.

This tutorial will show you some ways to move files from one directory to another in Python.


shutil.move()Moving files using functions in Python

shutilThe module is a Python module that helps in performing high-level operations on a file or a set of files. This module plays a role in operations like copying a file from somewhere or deleting a file.

shutilTo move a file from one directory to another with the help of the module, call shutil.move().

example:

import shutil
import os

file_source = "Path/Of/Directory"
file_destination = "Path/Of/Directory"

get_files = os.listdir(file_source)

for g in get_files:
    shutil.move(file_source + g, file_destination)

Here, listdir()the function from osthe module is used to get a complete list of all the files present in a directory. We use forthe loop to move the files and note that the function shutilof the module move()is used to transfer files from one directory to another.


os.rename()Use the or os.replace()function to move files in Python

There are many times when a user needs to connect to the main system through Python. In such situations, osmodules come into play. osModules basically act as an intermediary between the user and the computer's operating system so that the user can connect with the operating system properly.

One of the functions of this module is the module which is used to move files from one location to another rename(). This function moves the files by renaming the directory names of those files.

Another function of this module is replace()the rename function. This function helps in renaming a file or the current directory. The target must be a file and not a directory. So, if the target is a file, then it will be replaced without any error.

In summary, the function is used when the final destination of the file is in the same disk as its source rename(). If the destination of the file must be changed, it must be used replace().

example:

import os

file_source = "Path/Of/Directory"
file_destination = "Path/Of/Directory"

get_files = os.listdir(file_source)

for g in get_files:
    os.replace(file_source + g, file_destination + g)

Here too, we follow the same process by first defining the path to the initial and final directories. Then we use listdir()the function to get a list of all the files in the current directory. After that, we use forthe loop to overwrite the destinations of these files.


pathlibMoving files in Python using the module

The module in Python pathlibis a standard module that provides objects for manipulating different files and dictionaries. The core object for working with files is called path.

example:

from pathlib import Path
import shutil
import os

file_source = "Path/Of/Directory"
file_destination = "Path/Of/Directory"

for file in Path(file_source).glob("randomfile.txt"):
    shutil.move(os.path.join(file_source, file), file_destination)

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

Writing logs to a file in Python

Publish Date:2025/05/06 Views:132 Category:Python

This tutorial will show you how to write logs to files in Python. Use the module in Python logging to write logs to files Logging is used to debug a program and find out what went wrong. logging The log module is used to log data to a file

Comparing two dates in Python

Publish Date:2025/05/06 Views:97 Category:Python

This tutorial explains how to compare two dates in Python. There are multiple ways to determine which date is greater, so the tutorial also lists different sample codes to illustrate the different methods. Comparing two dates in Python usin

Reload or unimport modules in Python

Publish Date:2025/05/06 Views:58 Category:Python

Modules allow us to store definitions of different functions and classes in Python files, which can then be used in other files. pandas , NumPy , scipy , Matplotlib are the most widely used modules in Python. We can also create our own modu

Pausing program execution in Python

Publish Date:2025/05/06 Views:156 Category:Python

This tutorial will demonstrate various ways to pause a program in Python. Pausing the execution of a program or application is used in different scenarios, such as when a program requires user input. We may also need to pause the program fo

Importing modules from a subdirectory in Python

Publish Date:2025/05/06 Views:190 Category:Python

This tutorial will explain various ways to import modules from subdirectories in Python. Suppose we have a file in a subdirectory of our project directory and we want to import this file and use its methods in our code. We can import files

Sleeping for a number of milliseconds in Python

Publish Date:2025/05/06 Views:124 Category:Python

In this tutorial, we will look at various ways to pause or suspend the execution of a program in Python for a given amount of time. Let's say we want to pause the execution of a program for a few seconds to let the user read instructions ab

Python Numpy.pad Function

Publish Date:2025/05/06 Views:104 Category:Python

In Python, we have NumPy the array module to create and use arrays. Arrays can have different sizes and dimensions. Padding is a useful method that can be used to compensate for the size of an array. We can mutate an array and add some padd

Generating Random Colors in Python

Publish Date:2025/05/06 Views:135 Category:Python

In the digital world, colors are represented in different formats. RGB, Hexadecimal format are just some of the commonly used formats. In this tutorial, we will learn how to generate random colors in Python. When we talk about generating ra

Getting the name of a class in Python

Publish Date:2025/05/06 Views:83 Category:Python

Just like object constructors, classes can be defined as user-defined prototypes for creating objects. class Classes can be created using the keyword . A class is a data structure that can contain both data members and member methods. This

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial