How to Copy a File in Python?

srinivasr

Srinivas Ramakrishna

Posted on September 18, 2021

How to Copy a File in Python?

ItsMyCode |

We can copy a file in Python using shutil, os, and subprocess modules. Let’s take a look at each of these modules and the functions it has with examples.

Modules to copy a file in Python

  • shutil module
  • os module
  • subprocess module

shutil module top copy a file in Python

shutil module offers several easy-to-use copy functions to copy a file in Python.

copy()

The copy() method copies the source file to the destination file or directory. It copies the file data and file permission and returns the newly created file path. The input should be a path-like object or string.

# Copy a file in Python using copy() method
import shutil
shutil.copy( '/src/test/source.txt' , '/dest/destination.txt' )
Enter fullscreen mode Exit fullscreen mode

copy2()

The copy2() method is identical to the copy method, and in addition to that, it also preserves all the source file metadata.

However, this functionality is not available on all operating systems. On the platforms where this functionality is unavailable, copy2 will preserve all the metadata without raising any exception, even if it cannot preserve all the metadata.

# Copy a file in Python using copy2() method
import shutil
shutil.copy2( '/src/test/source.txt' , '/dest/destination.txt' )
Enter fullscreen mode Exit fullscreen mode

copyfile()

The copyfile() method copies the source file’s contents to the destination file. The destination file should be writeable, and it should not have the same name as the source file else, it will raise SameFileError

# Copy a file in Python using copyfile() method
import shutil
shutil.copyfile( 'source.txt' , 'destination.txt' )
Enter fullscreen mode Exit fullscreen mode

copyfileobj()

The copyfileobj() copies the content of the source file to the target file using the file object. By default, this method copies the data in chunks, and we can also specify the buffer size through the length parameter.

# Copy a file in Python using copyfileobj() method
import shutil
src_file=open('source.txt', 'rb')
dest_file= open('target.txt' , 'wb')
shutil.copyfileobj( src_file , dest_file )
Enter fullscreen mode Exit fullscreen mode

os module to copy a file in Python

popen()

The popen() method creates a pipe to the command, cmd. The method returns a file-like object connected to the cmd pipe. The command works the same as how we execute a command in the terminal window.

On windows

# Copy a file in Python using popen() method
import os
os.popen('copy source.txt destination.txt' )
Enter fullscreen mode Exit fullscreen mode

On Linux


# Copy a file in Python using popen() method
import os
os.popen('cp source.txt destination.txt' )
Enter fullscreen mode Exit fullscreen mode

system()

The system() method executes the specified command argument in a subshell. The return value of the system() method depends on the platform that runs the program.

On windows

# Copy a file in Python using system() method
import os
os.system('copy source.txt destination.txt' )
Enter fullscreen mode Exit fullscreen mode

On Linux


# Copy a file in Python using system() method
import os
os.system('cp source.txt destination.txt' )
Enter fullscreen mode Exit fullscreen mode

subprocess module to copy a file in Python

*call() *

The*call()*method is the recommended way of executing a command from the operating system.

Syntax – subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)

The args command includes the shell command that needs to be executed. By default, the shell argument is passed as false. If you pass this as true, it may cause a security risk.

On windows

# Copy a file in Python using call() method
import subprocess
subprocess.call('copy source.txt destination.txt', shell=True )
Enter fullscreen mode Exit fullscreen mode

On Linux


# Copy a file in Python using call() method
import subprocess
subprocess.call('cp source.txt destination.txt', shell=True )
Enter fullscreen mode Exit fullscreen mode

check_output()

This*check_output()* method allows us to execute a command within a shell. It is very much similar to the subprocess.run command, except that by default, it pipes data from stdout as encoded bytes.

On windows

# Copy a file in Python using ceck_output() method
import subprocess
subprocess.check_output('copy source.txt destination.txt', shell=True)
Enter fullscreen mode Exit fullscreen mode

On Linux


# Copy a file in Python using check_output() method
import subprocess
subprocess.check_output('cp source.txt destination.txt', shell=True )
Enter fullscreen mode Exit fullscreen mode

The post How to Copy a File in Python? appeared first on ItsMyCode.

💖 💪 🙅 🚩
srinivasr
Srinivas Ramakrishna

Posted on September 18, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Python String lstrip()
python Python String lstrip()

January 25, 2022

Python String rstrip()
python Python String rstrip()

January 25, 2022

Python String strip()
python Python String strip()

January 25, 2022