File Operations in Python 2 - shutil module

1 file operations

  There are a lot of file operations, file operations before the introduction of the built-in functions for file open, read and write operations are three basic use of the file. There are file copy, delete, move, change file owner is a group of other operations. The following method mainly depends os module and file module shutil operation.

2 os module

  os module is also open, read, write method, but the level is too low, it is recommended to use built-in functions open, read, write, both using similar methods. Here are the properties and methods for file operations os module provides:

Property or method Features
os.name Returns the type of operating system, windows are nt, Linux is posix.
os.uname() Return operating system information, * nix support.
os.listdir ( 'o: / temp') Returns the directory contents list.

os.stat(path, *, dir_fd=None,

follow_symlinks=Ture)

The essence of the method is to call the linux system stat command.

path: string or bytes path or file descriptor fd.

follow_symlinks: True returns the file information itself, False if it is soft and links

Soft link itself is displayed.

os.chmod(path, mode, *, dir_fd=None,

follow_symlinks=True)

Changed file permissions, e.g. os.chmod ( 'test', 0o777)
os.chown(path, uid, gid) Owner, is a group of the file, but requires sufficient permissions

 

3 shutil module

  Files by the method previously described operation, using the file open two objects, to read the contents of the source file, then the contents written to the target file in the file to complete the copy process. However, this data will be lost stat information (permissions, etc.), because there is no copy this information in the past. Python provides an advanced file operations shutil library, look at the following, a method of operating a file shutil provided in the library.

3.1 copy Copy

  • copyfileobj(fsrc, fdst[, length])

  Copy the file object, fsrc and fdst is an open file object open, copy the contents. fdst requirements writable. Which specifies the length represents the size of the buffer.

. 1  Import the shutil
 2  
. 3 with Open ( ' test.txt ' , ' W + ' ) AS F1:
 . 4      f1.write ( ' ABCD \ N1234 ' )
 . 5      f1.flush ()
 . 6      Print (f1.tell ())
 . 7      F1. Seek (0)   # after the file f1 is written content file pointer will point to the end of the file, then the file pointer to a file header want to complete the following copying process 
. 8      with Open ( ' the test1.txt ' , ' W + ' ) AS F2:
 . 9          shutil.copyfileobj (F1, F2)
View Code
  • copyfile(src, dst, *, follow_symlinks=True)

  Copy the contents of the file does not contain metadata. src, dst file path string, the return destination file path. Call essentially copyfileobj, it is not content with metadata binary copy. Note: copy the way will overwrite the destination file.

1 f = shutil.copyfile('test.txt', 'test1.txt')
2 print(f)
View Code
  • copymode(src, dst, *, follow_symlinks=True)

  Just copy file permissions.

1 shutil.copymode ( ' test.txt ' , ' test1.txt ' )
 2  print (os.stat ( ' test.txt ' ))
 3  print (os.stat ( ' test1.txt ' ))
View Code
  • copystat(src, dst, *, follow_symlinks=True)

  Copy metadata, stat include permission.

  • copy(src, dst, *, follow_symlinks=True)

  Copy the contents of the documents, permissions, and part of the metadata, not including creation and modification times. Call essentially copyfile and copymode.

  • copy2(src, dst, *, follow_symlinks=True)

  copy2 more copy all metadata, but requires platform support than copy. Copyfile and copystat calls on ability.

  • copytree(src, dst, *, symlinks=False, ignore=None, copy_function=copy2, ignore_dangling_symlinks=False)

  Recursive copy directories. Default copy2, that is, with a more diverse replicated data. the src, dst must be a directory, src must exist, dst must not exist; ignore = func, provides callable (src, names) -> ignored_names. Provide a function that will be called. src is the source directory, names the result of the os.listdir (src), that is, the file names are listed in src, the data type of the return value is set to be filtered file.

. 1  # O: / have a lower temp, b directory 
2  DEF the ignore (the src, names):
 . 3      IG = filter ( the lambda X: x.startswith ( ' a ' ), names)     # ignore a 
. 4      return SET (IG)
 . 5  
. 6 shutil.copytree ( ' O: / TEMP ' , ' O: / TT / O ' , the ignore the ignore =)
View Code

3.2 rm delete

  • shutil.rmtree(path, ignore_errors=False, onerror=None)

  Recursive delete. As rm -rf as dangerous, caution. rmtree not atomic, it is possible to remove the error, it will be suspended and it has been deleted deleted. ignore_errors is true, ignore the error. When is False or omitted onerror into effect; the onerror is callable, the function receiving function, path and execinfo.

shutil.rmtree ( ' O: / tmp ' )     # Similar rm -rf

3.3 move move

  •  move(src, dst, copy_function=copy2)

  Recursive move a file, directory to the target path, return to the target path. Its essence is os.rename method used, if not support rename and move is the first copytree directory and then delete the source directory. Copy2 default method.

Guess you like

Origin www.cnblogs.com/dabric/p/11757057.html