程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Yyds dry inventory advanced file processing of Python usage skills

編輯:Python

shutil There's a lot of copy Class . One of the most used is copy and copytree. The former is used to copy a file , The latter is used to copy a directory .shutil Module move(src, dst) Used to set the path src The file at is moved to dst It's about , And return the absolute path of the new location .

shutil.rmtree Whether the directory is not empty or not , Delete the entire directory directly .

import
shutil


shutil. copy( 'log.log', 'log2.log') # Copy files
shutil. copytree( 'work1', 'work2') # Copy folder
shutil. move( 'log2.log', 'log3.log') # Moving files , You can also move folders
shutil. rmtree( 'dir') # Delete directory , The directory can not be empty
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

filecmp The simplest function of the module is cmp function , This function is used to compare whether two files are the same , If the files are the same , return True, Otherwise return to False. The contents of the documents must be exactly the same , Multiple spaces will return False.

filecmp There is also a directory called cmpfiles Function of , This function is used to compare multiple files in two different directories at the same time , And returns a triple , Each contains the same file 、 Different files and files that cannot be compared .

dircmp Is used to compare two directories . Return to one dircmp Class object , This object holds many properties .dircmp The function just compares the files and subdirectories under the directory , It does not recursively compare the contents of subdirectories . For directory ,dircmp The function is just the name of the comparison function , I won't compare the contents of subdirectories

import
filecmp


print( filecmp. cmp( 'log.log', 'log2.log'))
print( filecmp. cmpfiles( 'files', 'test',[ 'copy.py']))
d = filecmp. dircmp( 'files', 'test')
print( d. report())
print( d. left_list) # The files contained in the directory on the left
print( d. right_list) # The files contained in the directory on the right
print( d. left_only) # On the left , Not on the right
print( d. right_only) # On the right , Not on the left
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

How to use filecmp The module compares files and directories , although filecmp It is easy to compare the use of files and directories , But there are many situations that it can't handle . for example , Find all the same files in the current directory and subdirectory , Compare whether the files on different computers are the same . Simply compare whether the two files are equal or whether the files in the two directories are different , Many times it can not meet our needs . This is the time , You can use the check code (chucksum) Compare files in the same way .  

Check code is calculated by hash function , Is a way to create small numbers from any data “ The fingerprint ” Methods . Hash functions compress messages or data into digests , Make the amount of data smaller , Easy to compare .MD5 Is currently the most widely used hash algorithm . In theory , One MD5 The hash value can correspond to an infinite number of files , But from a realistic point of view , It is almost impossible for two different files to have the same MD5 Hash value , Any non malicious change to a file will cause it to MD5 Han Xi value change . therefore ,MD5 Hashes are generally used to check file integrity , It is especially used to detect file transfers 、 Disk error or the correctness of the file in other cases .

stay Python In the calculation file MD5 The check code is also very simple , Use... From the standard library hashlib The module can .

import
hashlib


h = hashlib. md5()
with open( 'log.log') as f:
for line in f:
h. update( bytes( line, encoding = 'utf-8'))
print( h. hexdigest())
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved