Python implements file copy and paste

The 9 ways to copy files with Python are:

shutil copyfile() 
shutil copy() 
shutil copyfileobj() 
shutil copy2()
os popen()
os system() 
threading Thread() 
subprocess call() 
subprocess check_output() 

1.Shutil Copyfile()
1. Only when the target is writable, this method will copy the source content to the target location. If you do not have write permission, it will cause an IOError exception.

copyfile(source_file, destination_file)
copies the source content to the destination file.

2. It copies the source content to the target file. If the target file is not writable, the copy operation will cause an IOError.

3. If the source file and target file are the same, it will return SameFileError.

4. However, if the target file has a different name before, the copy will overwrite its content.

5. If the target is a directory, which means that this method will not copy to the directory, then Error 13 will occur.

6. It does not support copying files such as character or block drives and pipes.

from shutil import copyfile
from sys import exit

source = input("Enter source file with full path: ")
target = input("Enter target file with full path: ")

# adding exception handling
try:
   copyfile(source, target)
except IOError as e:
   print("Unable to copy file. %s" % e)
   exit(1)
except:
   print("Unexpected error:", sys.exc_info())
   exit(1)

print("\nFile copy done!\n")

2.Shutil Copy() method

copyfile(source_file, [destination_file or dest_dir])

The function of the copy() method is similar to the "cp" command in Unix. This means that if the target is a folder, then it will create a new file in it with the same name (base name) as the source file. In addition, this method will synchronize the permissions of the target file to the source file after copying the content of the source file.

import os
import shutil

source = 'current/test/test.py'
target = '/prod/new'

assert not os.path.isabs(source)
target = os.path.join(target, os.path.dirname(source))

# create the folders if not already exists
os.makedirs(target)

# adding exception handling
try:
   shutil.copy(source, target)
except IOError as e:
   print("Unable to copy file. %s" % e)
except:
   print("Unexpected error:", sys.exc_info())

copy() vs copyfile() :

1.copy() can also set permission bits when copying content, while copyfile() only copies data.

2. If the target is a directory, copy() will copy the file, and copyfile() will fail with Error 13.

3. The copyfile() method uses the copyfileobj() method in the implementation process, and the copy() method uses the copyfile() and copymode() functions in turn.

3. Shutil Copyfileobj () method

This method copies the file to the target path or file object. If the target is a file object, then you need to close it after calling copyfileobj(). It also assumes an optional parameter (buffer size) that you can use to set the buffer length. This is the number of bytes saved in memory during the copy process. The default size used by the system is 16KB.

from shutil import copyfileobj
status = False
if isinstance(target, string_types):
   target = open(target, 'wb')
   status = True
try:
   copyfileobj(self.stream, target, buffer_size)
finally:
   if status:
       target.close()

4.Shutil Copy2 () 方法

Although the function of the copy2() method is similar to copy(). But it can get the access and modification time added in the metadata when copying the data. Copying the same file will cause a SameFileError exception.

copy() vs copy2():
1.copy() can only set permission bits, and copy2() can also use timestamps to update file metadata.
2. copy() calls copyfile() and copymode() inside the function, and copy2() calls copystat() to replace copymode().

5.Os Popen() method
This method creates a pipe for sending or receiving commands. It returns an open file object that is connected to the pipe. You can use it for reading or writing according to the file open mode such as'r' (default) or'w'.

os.popen(command[, mode[, bufsize]])

import os
os.popen('copy 1.txt.py 2.txt.py')

6.Os System() method

This is the most common way to run any system command. Using the system() method, you can call any command in the subshell. Internally, this method will call the standard library functions of the C language.
This method returns the exit status of the command.

import os
os.system('copy 1.txt.py 2.txt.py') 

7. Use the asynchronous thread library threading Thread() to copy files

If you want to copy files asynchronously, then use the following method. Here, we use Python's threading module to perform copy operations in the background.

When using this method, make sure to use locks to avoid deadlocks. If your application uses multiple threads to read/write files, you may encounter this situation.

import shutil
from threading import Thread

src="1.txt.py"
dst="3.txt.py"

Thread(target=shutil.copy, args=[src, dst]).start()

8.Subprocess Call()

The Subprocess module provides a simple interface to handle subprocesses. It allows us to start the child process, connect to the input/output/error pipe of the child process, and retrieve the return value.

The subprocess module is designed to replace older modules and functions, such as – os.system, os.spawn*, os.popen*, popen2.*

It uses the call() method to call system commands to perform user tasks.

import subprocess

src="1.txt.py"
dst="2.txt.py"
cmd='copy "%s" "%s"' % (src, dst)

status = subprocess.call(cmd, shell=True)

if status != 0:
    if status < 0:
        print("Killed by signal", status)
    else:
        print("Command failed with return code - ", status)
else:
    print('Execution of %s passed!\n' % cmd)

** subprocess Check_output() **

Using the Check_output() method in subprocess, you can run an external command or program and capture its output. It also supports pipes.

import os, subprocess

src=os.path.realpath(os.getcwd() + "http://cdn.techbeamers.com/1.txt.py")
dst=os.path.realpath(os.getcwd() + "http://cdn.techbeamers.com/2.txt.py")
cmd='copy "%s" "%s"' % (src, dst)

status = subprocess.check_output(['copy', src, dst], shell=True)

print("status: ", status.decode('utf-8'))

Guess you like

Origin blog.csdn.net/liulanba/article/details/114980792