Copy file in Python

In this post, we are going to learn how to Copy files in Python with examples. We are going to use the Python shutile, os, subprocess module methods to copy file.

1. OS Module to copy file in python


The os module provides functions to handle the directory and files of the operating system. The os module is part of the standard library so no installation is required for this. We directly import in our program.

1.1 Using Os.popen() 


The Os.popen() method allows us to open a pipe from a command and interact with the operating system as we do on the command prompt mainly works to call another program by using python code. It can be used for UNIX and window

  • To use this method,First we have to import OS Module by using import os
  • Call os.popen() method by passing a command that we want to run “copy stu.txt studata.txt”..

Python Program to copy a file

import os
cmd = os.popen('copy stu.txt studata.txt' )


1.2 Using os.system()


The os. system() execute a command in a subshell. Let us understand with an example

  • To use this method,First we have to import OS Module by using import os
  • Call os.system() method by pass command as string “copy stu.txt studata.txt”

Python Program Example

import os
os.System('copy stu.txt studata.txt' )
print(cmd.read())

Output

  1 file(s) copied.

2. Using shutile Module to copy file in Python


The python shutile module allows us to perform high-level operations on files. The specific function is provided for removing and copying files. We are going to use the function provided for copying files.

2.1 shutile.copyfileobj()


It copies the contents of the source file to the destination file by using the file object. The contents of the file are copied in chunks to avoid memory consumption.

Syntax

shutil.copyfileobj(srcfobj, dstfobj,length])
  • srcfobj : source file object
  • dstfobj : Destination file object can point to file or directory.
  • Legth :It default value is 16KB.The buffer length can be increase

Python Program Example to copy file using shutile.copyfileobj()

import shutil
src_Fobj=open('geek.txt', 'rb')
des_Fobj= open('C:/Users/Admin/Desktop/studata.txt' , 'wb')
shutil.copyfileobj( src_Fobj , des_Fobj )

src_Fobj.close()
des_Fobj.close()

2.2 shutil.copyfile()


The shutil.copyfile() method copy the contents of the source file to the destination file. If we do not have permission to write on the destination file otherwise PermissionError will raise.

Syntax

shutil.copyfileobj(srcFPath, dstF_path)

Parameters

  • srcF_Path : The source filename with the full path that we want to copy.
  • dstF_Path : The destination filename with full path that we want to copy.It can be file or directory paths

Python program to cop file using shutile.copyfile()

import shutil

shutil.copyfile('geek.txt', 'C:/Users/Admin/Desktop/studata.txt' )

2.3 shutil.copy()


The shutil.copy() method copies the source file to the destination path. if the directory path is passed as the destination then the source file will copy to the destination directory with the same filename.

Syntax

shutil.copyfileobj(srcF_Path, dstF_path)

Parameters

  • srcF_Path : The source filename with the full path that we want to copy.
  • dstF_Path : The destination filename with full path that we want to copy

Python Program Example

import shutil

shutil.copy('geek.txt', 'C:/Users/Admin/Desktop/' )

3. Using Subprocess Module to copy file in python


We can use Subprocess.call() method to copy file in python by executing a command that passes as an argument to call() method.

Python Program Example

import subprocess
subprocess.call('copy geek.txt studata.txt', shell=True)


Summary

In this post, we have learnt how to Copy file in Python using many ways that are helpful while working with file handling operation in Python.