[ TOC]
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;
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
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