Nine kinds of methods to copy files Python

The following is a demonstration of "how to copy files in Python" of nine kinds of methods .

  1. shutil copyfile () method
  2. shutil copy () method
  3. shutil copyfileobj () method
  4. shutil copy2 () 方法
  5. os popen method
  6. os system () method
  7. Thread () method
  8. Child calls () method
  9. Child process check_output () method

Nine kinds of methods to copy files Python

1. Shutil Copyfile () method

The only method of copying the contents of the source to the destination only when the target is writable. If you do not have write permission is thrown IOError .

It is read by opening the input file, ignoring its file type. Next, it will not be any different treatment for special files, nor will they copy the new special file.

Said in the CopyFile () method with a lower-level function copyfileobj () below. It will file name as an argument, and they open file handle is passed to copyfileobj () . This method has an optional third parameter, you can use to specify a buffer length. It will then open the file specified in the read block buffer size. However, the default behavior is a one-time read the entire file.

Nine kinds of methods to copy files Python
If you are still confused in the programming world, you can join us to learn Python buckle qun: 784758214, look at how seniors are learning. Exchange of experience. From basic web development python script to, reptiles, django, data mining and other projects to combat zero-based data are finishing. Given to every little python partner! Share some learning methods and need to pay attention to small details, click on Join us python learner gathering

The following are CopyFile () point method.

  • It will copy the contents of the source to a file named target.
  • If the destination is not writable, the copy operation will result IOError exception.
  • If the source and destination files are the same, it will return SameFileError .
  • However, if the destination under a different name pre-existing, a copy will overwrite its contents.
  • If the destination is a directory, it means that this method will not be copied to a directory, an error will occur 13.
  • It does not support copy files, such as a character or block and pipelines.

Nine kinds of methods to copy files Python

2. Shutil Copy () method

Nine kinds of methods to copy files Python

copy () function is similar to the method of "CP" in the Unix command. This means that if the destination is a folder, where it will create a source file with the same name ( basename new file). In addition, the method to copy its contents permissions will target the source files to be synchronized. If you want to copy the same files, it can also throw SameFileError .

Nine kinds of methods to copy files Python

Nine kinds of methods to copy files Python

3. Shutil Copyfileobj () method

This method copies the file to the target path or file object. If the target is a file object, you need to call copyfileobj () after explicit close it. It is assumed that an optional parameter (buffer size), you can use it to provide buffer length. This is the number of bytes stored in the memory during replication. The system uses the default size is 16KB.

Nine kinds of methods to copy files Python

4. Shutil Copy2 () 方法

However, copy2 () function method is similar to Copy () . However, at the same time to copy the data, you can also add the access and modification times in the metadata. Copy the same file can cause SameFileError .

Nine kinds of methods to copy files Python

For curious - the difference between () CopyMode () and Copystat.

Nine kinds of methods to copy files Python

5. Os Popen()方法

该方法创建一个指向或来自该命令的管道。它返回一个连接到管道的打开的文件对象。您可以根据文件打开模式(即’r’(默认)或’w’)使用它进行读取或写入。

Nine kinds of methods to copy files Python

  • 模式 –它可以是’r’(默认)或’w’。
  • bufsize –如果其值为0,则不会发生缓冲。如果设置为1,则在访问文件时将进行行缓冲。如果您提供的值大于1,则缓冲将以指定的缓冲区大小进行。但是,对于负值,系统将采用默认缓冲区大小。

对于Windows操作系统。

Nine kinds of methods to copy files Python

对于Linux操作系统。

Nine kinds of methods to copy files Python

6. Os System()方法

它是运行任何系统命令的最常见方式。使用system()方法,可以在subshell中调用任何命令。在内部,这种方法会调用标准的C库函数。

此方法返回命令的退出状态。

对于Windows操作系统。

Nine kinds of methods to copy files Python

对于Linux操作系统。

Nine kinds of methods to copy files Python

7.以Async方式使用线程库的Python文件复制

如果要以异步方式复制文件,请使用以下方法。在这里,我们使用Python的线程模块在后台运行复制操作。

使用此方法时,请确保采用锁定以避免死锁。如果您的应用程序正在使用多个线程读取/写入文件,您可能会面对它。

Nine kinds of methods to copy files Python

8.使用子进程的Call()方法在Python中复制一个文件

子进程模块提供了一个简单的界面来处理子进程。它使我们能够启动子进程,附加到其输入/输出/错误管道,并检索返回值。

子流程模块旨在替代传统模块和功能,如*os.system,os.spawn ,os.popen *,popen2。***。

它公开一个call()方法来调用系统命令来执行用户任务。

Nine kinds of methods to copy files Python

9.使用子进程的Check_output()方法在Python中复制文件

Use sub- process of check_output () method, you can run an external command or program and capture its output. It also supports the pipeline.

Nine kinds of methods to copy files Python

Guess you like

Origin blog.51cto.com/14510224/2438067