Python is a versatile programming language that can be used for various purposes. From Data Analytics to Web Scraping to Machine Learning, Python has libraries for any task that a program can do. One such important task is to copy a file in Python from a directory and paste it into another.
Copying and pasting files can be performed through the operating system. But the OSs are written in a certain programming language and the code accesses the files from the filesystem whenever we perform such operations. Similarly, we can write code to do the task of copying instead of doing it through operating systems.
How to Copy a file in Python?
Filesystem manipulation is highly used in almost all the use cases of advanced programming and development. Preparing datasets for an ML Pipeline, Storing data in the filesystem for software or a website, etc, all of these require a solid understanding of file systems.
There are different ways for copying a file in Python. We will start with an approach of using the library shutil.
Using shutil.copy() in shutil library
The shutil library offers many high-level file operations such as copy, create, remote, etc. Shutil is a built-in library meaning you do not need to use pip to install Shutil.
The way it works is via accessing the command line of the operating system or the shell of the operating system. The name ‘Shutil’ comes from the term ‘shell utilities’. Shells can be used to manipulate filesystems and Shutil does exactly that.
Here’s the code demonstrating how to copy a file from one location to another using shutil:
In the above code, we copied a file called file.txt from the desktop folder and we pasted it into the documents folder via the shutil.copy() function. shutil.copy() function accepts two arguments, the source path, the original path of the file, and the destination path, the path to which the file needs to be copied.
The task of copying is as simple as that and Shutil library provides other functions that provide different uses.
copy2() function is more useful than copy() function as it allows the destination folder to be a directory instead of the complete target filename. It also preserves the original modification and access info in the metadata of the file. Here’s an example:
In this example, the destination path is shorter allowing for lesser code which is always good. Shutil also provides functions such as copyfileobj() and copyfile() that essentially work the same way with different caveats. Here are the implementations of those functions:
The significant difference for using copyfile() function copies a file from one path to another in the most efficient way possible. The copy() and copy2() functions can be used to copy directories from one path to another but copyfile() function only copies the file from one path to another and the function is designed to do the task in the most efficient way.
copyfileobj() function copies the file-like objects meaning they are only useful if the files are file objects. File objects can be created by the function open() with a parameter ‘r’ or ‘w’ depending on if the file needs to be read or written.
The copyfileobj() function is a comparatively convoluted way of copying a file from another but it’s useful if files are already being accessed as file objects. Here is an example:
Earlier, it was mentioned that Shutil essentially works by accessing the command line of the operating system. Similarly, we can skip the middle-man and directly access the command line and copy a file from one destination to another in python through the os module.
Using os module
The os module provides a way to use the functionalities of the operating systems through python. The operations that we can perform through this module include opening a file, accessing file data, writing command line arguments, and accessing environment variables. In our case, we need to write a command line argument from python using the os library.
Here’s the source code for Windows:
Here’s the source code for Windows: for Linux/Unix or Mac:
We notice that if we were to directly access the command line, the commands are platform-dependent and operating system dependent, meaning each operating system requires different commands.
Shutil module takes care of the platform dependencies and operating system dependencies and it abstracts the process of writing a command making it much simpler to use. If we were to write a program that should work on any operating system, choosing Shutil over the os module method is better.
Similar to the os module, there is another module through which we can pass command line commands called the subprocess module.
Using subprocess module
The subprocess module is used to create new processes in the operating systems. One such process can be the process of calling a command. The syntax is essentially similar to the os module implementation but the method is different and this method requires an additional parameter.
Here is how to implement it for Windows:
Here is how to implement it for Linux/Unix or Mac:
The functionality is the same as os but there’s an additional parameter called shell which should be the boolean value True in order food the command to be passed as a shell or a command line command.
In conclusion, we have learned different ways to copy a file from one place to another in python and we have built a solid understanding of different modules that manipulate file systems. Also, learn how to get a list of files in a directory in python. It is also important and connected to this operation.