Python automated operation and maintenance 2

[ TOC]

Configuration file

yaml configuration#####

Description: What the yaml configuration file has in common with the xml configuration file and the json configuration file is that it is easy to understand and use, and the configuration file is more concise and easy to understand;

ruamel.yaml module######

Official website link: https://pypi.org/project/ruamel.yaml/

#! /usr/bin/env python
# - *- coding: utf-8-*-
# @ File : YamlDemo.py
# @ CreateTime :2019/7/2411:07
# @ Author : WeiyiGeek
# @ Function :Yaml language analysis
# @ Software: PyCharm
# pip install ruamel.yaml

import sys
import ruamel.yaml

def main():
 #(1) Read Yaml configuration file information
 yaml = ruamel.yaml.YAML()withopen('demo.yaml','r',encoding='utf-8')as conf:try:
   config = yaml.load(conf)
  except ruamel.yaml.YAMLError as e:print("Parsing error:",e)

 #(2) Print out the yaml configuration file information constant scales
 for i in config:print(i," : ", config[i])
 # Object
 for i in config['obj']:print(i, config['obj'][i])
 # Array
 print(config['array'][0]['key1'], config['array'][1]['key2'], config['array'][2]['key3'])

 #(3) Modify and add
 config['NAME']="WeiyiGeek"
 config['add']="WeiyiGeek"
 config['obj']['age']="888"
 config['obj']['love'][1]="cooking"

 #(4) Write to archive
 withopen("update.yml",'w+',encoding='utf-8')as con:
  ruamel.yaml.dump(config, con, allow_unicode=True, Dumper=ruamel.yaml.RoundTripDumper)  #Plus what style Dumper was originally like
  ruamel.yaml.dump(config, sys.stdout, Dumper=ruamel.yaml.RoundTripDumper)  #Output to terminal after modification

if __name__ =='__main__':main()

operation result:

# Print result
PI  :  3.1415926
NAME  :  “This is a String”
INT  :  1024
obj  :  ordereddict([('name','WeiyiGeeK'),('age',18),('love',['Computer','Cook','car'])])
array  :  [ordereddict([('key1','I')]),ordereddict([('key2','Love')]),ordereddict([('key3','Study')])]
name WeiyiGeeK
age 18
love ['Computer','Cook','car']
I Love Stud

# yaml write file result
# constant
PI:3.1415926
NAME: WeiyiGeek
INT:1024
# Object(set)
obj:
 name: WeiyiGeeK
 age:'888'
 love:!!obj
 - Computer
 - "\ u70F9\u996A"- car
# Array
array:- key1: I
- key2: Love
- key3: Study
add: WeiyiGeek

reference

ini configuration#####

File backup

Description: Use the filecmp module to confirm whether the backup directory is consistent with the source directory file, and synchronize the source directory file to the target directory:

#! /usr/bin/env python
# - *- coding: utf-8-*-
# @ File : verity.py
# @ CreateTime :2019/7/2614:23
# @ Author : WeiyiGeek
# @ Function :Synchronously verify source files/Target file
# @ Software: PyCharm

import os,sys
import filecmp
import shutil

holderlist =[]
destination =[]

def compareme(src, dest):
 dircomp = filecmp.dircmp(src, dest);
 onlyfile = dircomp.left_only    #Source file new directory or file
 difffile = dircomp.diff_files   #Files that have changed in the source directory (unmatched files)
 dirpath = os.path.abspath(src)

 # Write the file path where the variable occurs, using the lamba expression
 [ holderlist.append(os.path.abspath(os.path.join(src, x)))for x in onlyfile]  #Store the absolute path of the file created in the source file into the array
 [ holderlist.append(os.path.abspath(os.path.join(src, x)))for x in difffile]  #Change the file in the source file/The absolute path of the directory is stored in the array

 # src /desc files that exist on both sides(Continue recursive comparison)iflen(dircomp.common_dirs):for item in dircomp.common_dirs:compareme(os.path.abspath(os.path.join(src, item)), os.path.abspath(os.path.join(dest, item)))return holderlist

def main():
 global destination,\
  holderlist
 iflen(sys.argv)>2:
  src = sys.argv[1]
  dest = sys.argv[2]else:print("""
   Usage: verity.py srcDirectory destDirectory
        """)
  sys.exit(1)

 source_files =compareme(src, dest)  #Compare source directory and backup

 dir1 = os.path.abspath(src)
 dir2 = os.path.abspath(dest)

 createdir = False

 # Variables return differential files(After replacing the path, put it back into the destination array to prepare for the subsequent zip)for item in source_files:
  # Get the directory in the source file
  destination_dir = item.replace(dir1,dir2)
  destination.append(destination_dir)

  # Create a directory(If the directory does not exist)if os.path.isdir(item):if not os.path.exists(destination_dir):
    os.makedirs(destination_dir)
    createdir = True

 # If you create a directory, re-traverse the newly created directory
 if createdir:
  destination =[] #Prevent duplication
  srcfile =compareme(dir1,dir2)
  holderlist = srcfile
  for item in srcfile:
   destination.append(item.replace(dir1,dir2))[print(x)for x in destination]print("update item")
 copy_pair =zip(source_files, destination) #source/Split the backup directory file list into tuples
 for item in copy_pair:      #Determine file copy operation
  if os.path.isfile(item[0]):
   shutil.copyfile(item[0],item[1])print(item)if __name__ =='__main__':main()

WeiyiGeek.filecmp file directory verification

Recommended Posts

Python automated operation and maintenance 2
Python automated operation and maintenance 1
Python file operation
Python and Go
python operation kafka
Python file and directory operation code summary
Linux overview of Python automated operation and maintenance and the ultimate guide to virtual machine installation and use
Python introspection and reflection
[python] python2 and python3 under ubuntu
Python operation SQLite database
Python operation yaml instructions
Python deconstruction and packaging
Python3 configuration and entry.md
Python introduction and environment installation
Python operation Excel merge cells
Python know crawler and anti crawler
centos7 install python3 and ipython
Python negative modulus operation example
ubuntu18.04 compile and install python3.8
Centos 6.10 reinstall python and yum
Python open read and write
CentOS7 install python3 and pip3
Python data structure and algorithm
Python multi-process and multi-thread basics
CentOS 6.9 compile and install python
Quick start Python file operation
CentOS 6 compile and install python 3
Generators and iterators in Python
Python file read and write operations
Python and js interactive call method
Magic methods and uses of Python
Python judges positive and negative numbers
python ftp upload files and folders
Python crawler | Cognitive crawler request and response
Python preliminary implementation of word2vec operation
FM algorithm analysis and Python implementation
Common errors and solutions in python
CentOS7 installation and maintenance of Gitlab
Python implements string and number splicing
Python list comprehension operation example summary
Python file operation basic process analysis
Python function definition and parameter explanation
Implementation of python selenium operation cookie
CentOS quickly install Python3 and pip3
Mongodb and python interaction of python crawler
Install Python3 and ansible under CentOS8
Python processing PDF and CDF examples
Played with stocks and learned Python
Configure python3 environment on centos7 and
Some examples of python operation redis
Python reads and writes json files
Example operation of python access Alipay
Python implements username and password verification
Install Python3 and Py under CentOS7
The operation of python access hdfs
Python handles operation code to execl
Python basic syntax and number types
Python learning os module and usage