Home  >  Article  >  Backend Development  >  How to copy a file using python

How to copy a file using python

anonymity
anonymityOriginal
2019-05-25 10:06:2440329browse

How to use python to copy a file: 1. [copyfile(src, dst)]; 2. [copymode(src, dst)]; 3. [copystat(src, dst)]; 4. [ copy(src, dst)]; 5. [copy2 src].

How to copy a file using python

How to copy a file using python:

1. copyfileobj(fsrc, fdst, length =16*1024): Copy the contents of the fsrc file to the fdst file, length is the length of fsrc read each time, and is used as the buffer size

  • fsrc: Source file

  • fdst: Copy to fdst file

  • length: Buffer size, that is, the length of each fsrc read

import shutil
f1 = open("file.txt","r")
f2 = open("file_copy.txt","a+")
shutil.copyfileobj(f1,f2,length=1024)

2. copyfile(src, dst): Copy the contents of the src file to the dst file

  • src: Source file path

  • dst: Copy to dst file. If the dst file does not exist, a dst file will be generated; if it exists, it will be overwritten.

  • follow_symlinks : When set to True, if src is a soft link, it will be copied as a file; if set to False, the soft link will be copied. Default is True. Python3 new parameters

import shutil
shutil.copyfile("file.txt","file_copy.txt")

3, copymode(src, dst): Copy the src file permissions to the dst file. File content, owner and group are not affected

  • src: Source file path

  • dst: Copy permissions to dst file, dst path It must be a real path, and the file must exist, otherwise a file not found error will be reported

  • follow_symlinks: When set to False, src and dst are soft links, and you can copy the soft links. Connection permission, if set to True, will be treated as ordinary file copy permission. Default is True. Python3 new parameters

import shutil
shutil.copymode("file.txt","file_copy.txt")

4, copystat(src, dst): Copy permissions, last access time, last modification time and src flag to dst. File content, owner and group are not affected

  • src: Source file path

  • dst: Copy permissions to dst file, dst path It must be a real path, and the file must exist, otherwise a file not found error will be reported

  • follow_symlinks: When set to False, src and dst are soft links, and you can copy the soft links. The connection permissions, last access time, last modification time and src flag, if set to True, are treated as ordinary file copy permissions. Default is True. Python3 new parameters

import shutil
shutil.copystat("file.txt","file_copy.txt")

5, copy(src, dst): Copy the file src to dst. dst can be a directory, and a file with the same name as src will be created in the directory. If a file with the same name exists in the directory, an error will be reported indicating that a file with the same name already exists. Permissions will be copied as well. The essence is that copyfile and copymode are called successively

  • src: source file path

  • dst: copied to the dst folder or file

  • follow_symlinks: When set to False, src and dst are soft links, and soft link permissions can be copied. If set to True, it will be treated as ordinary file copy permissions. Default is True. Python3 new parameters

improt shutil,os
shutil.copy("file.txt","file_copy.txt")
# 或者
shutil.copy("file.txt",os.path.join(os.getcwd(),"copy"))

6, copy2(src, dst): Copy the file src to dst. dst can be a directory, and a file with the same name as src will be created in the directory. If a file with the same name exists in the directory, an error will be reported indicating that a file with the same name already exists. Permissions, last access time, last modification time and src flag will be copied to dst. The essence is that the copyfile and copystat methods are called successively

  • src: source file path

  • dst: copied to the dst folder or file

  • follow_symlinks: When set to False, src and dst are both soft links. You can copy the soft link permissions, last access time, last modification time and src flag. If set to True, It is treated as a normal file copy permission. Default is True. Python3 new parameters

improt shutil,os
shutil.copy2("file.txt","file_copy.txt")
# 或者
shutil.copy2("file.txt",os.path.join(os.getcwd(),"copy"))

Related learning recommendations:python tutorial

The above is the detailed content of How to copy a file using python. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn