23
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.
- shutil module
- os module
- subprocess module
shutil
module offers several easy-to-use copy functions to copy a file in Python.
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' )
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' )
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' )
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 )
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' )
On Linux
# Copy a file in Python using popen() method
import os
os.popen('cp source.txt destination.txt' )
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' )
On Linux
# Copy a file in Python using system() method
import os
os.system('cp source.txt destination.txt' )
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 )
On Linux
# Copy a file in Python using call() method
import subprocess
subprocess.call('cp source.txt destination.txt', shell=True )
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)
On Linux
# Copy a file in Python using check_output() method
import subprocess
subprocess.check_output('cp source.txt destination.txt', shell=True )
The post How to Copy a File in Python? appeared first on ItsMyCode.
23