/  Technology   /  How can a file be copied?  Part- III

How can a file be copied?  Part- III

How can a file be copied? Part- III

 

Using the subprocess module

In this article let’s learn ways to copy a file using the subprocess module. This module replaces some methods in the os module, in particular the os.system() method.Make sure you learn about the shutil module, the os module and their  methods before we dive into this article.

https://docs.google.com/document/d/19emAkEuRpePGT0JhSLnAj7-D-ba_LbBEc5DboealdRI/edit?usp=sharing

https://docs.google.com/document/d/1MAl2YzTck7LRpLY1zcWaI5gLaAJFuCxVMll6yR9Va60/edit?usp=sharing

This module has two main methods to access the operating system commands for file copying.

  • call()
  • check_output()

let’s discuss each one of these methods in detail.

 

1. Using call():

This method is much similar to the os.system() method which directly calls or runs the command passed as an argument to the function. We can launch a command directly from the operating system. 

The syntax of call() is as follows

args is the shell command used.

The use of shell=True would be a security risk as per the Python documentation.

2. Using check_output():

This method also executes a command within a shell It, by default pipes data from stdout as encoded bytes. We can run an external command or a program and get its output. 

The syntax of check_output() is as follows

args is the shell command used.

The use of shell=True would be a security risk as per the Python documentation.

 

For Linux:

For Windows:

Leave a comment