| by Arround The Web | No comments

Python OS Copy

In Python, the “os” module offer several methods to deal with or interact with the lower-level operating system. One such method is “os.system()”, which is utilized to execute the string-type command in a shell. We can utilize this method to copy the files from one particular location to another.

This write-up will explain a complete overview of copying files from one place to another using the Python “os” module. Follow the below contents to get started:

How to Copy a File Using the “os” Module in Python?

The “os.system()” method of the “os” module is utilized to copy a file from one specified location to another location. This method allows us to execute the shell commands or system commands by accepting the string as an argument.

Syntax

Check out the general syntax of the os.system method:

os.system(command)

Parameters

Here, the string type “command” parameter specifies the command that we want to execute in the shell.

Return Value

The  “os.system()” function retrieves the exit status of the command which depends on the operating system:

  • An exit status of the process for Unix.
  • The value returned by the system shell on Windows.

Example 1: Using the “os” Module to Copy a File

This example utilizes the “os” module to copy a file in Python. Here is the original file and its location:

In the below code, the “os” module is imported, and the source path and the final path are initialized in the program. After that, the “os.system()” takes the command as a string and executes it to copy the file from one place to another:

import os
source_path = r'C:\Users\p\Documents\program\sample2.txt'
final_path = r'C:\Users\p\Downloads\Linuxhint1/new.txt'
os.system(f'copy "{source_path}" "{final_path}"')

The above code successfully copies the file from one place to another, as shown in the below snippet:

Note: If you want to know other methods that are used to copy files in Python, then check How to Copy a File in Python article.

Example 2: Using the “os” Module to Copy Multiple Files

We can also copy multiple files using the “os.system()” method of the “os” module in Python. For example, consider the following multiple files and their path:

Take the following code to copy all the multiple files from one place to another particular place. The “os.listdir()” method lists all the files placed in the directory. The for loop iterates over the listed files and moves files from one location to another by using the “os.system()” method:

import os
source_path = r'C:\Users\p\Documents\program\'
final_path = r'
C:\Users\p\Downloads\Linuxhint1\'
for i in os.listdir(source_path):
    source = source_path + i
    dest = final_path + i
    if os.path.isfile(source):
        os.system(f'
copy "{source}" "{dest}"')

All the multiple files placed at the specified locations have been moved successfully:

Example 3: Using the “os” Module to Copy a File After Checking the Operating System

In the following example code, the “os.system()” copies the file after checking the operating system. The “if-else” statements are used to check the specified operating system and apply the specified command. At last, the “os.system()” takes the command as an argument and executes it in the shell.

import os
source_path = r'C:\Users\p\Documents\program\sample2.txt'
final_path = r'C:\Users\p\Downloads\Linuxhint1/new.txt'
if os.name == 'nt':
    cmd = f'copy "{source_path}" "{final_path}"'
else:
    cmd = f'cp "{source_path}" "{final_path}"'
os.system(cmd)

The above code successfully copies the specified file to the particular location:

Conclusion

The “os.system()” method of the “os” module that takes the string-type command as an argument and executes it to copy the files from one place to another. This method can be used to copy single or multiple files from one place to another. It can also be used with if-else blocks to check the operating system before copying the files. This tutorial delivered an in-depth guide on the Python OS copy method via several examples.

Share Button

Source: linuxhint.com

Leave a Reply