[TOC]
描述:yaml配置文件与xml配置文件以及json配置文件的共同之处是在于方便理解与使用,是配置文件更加的简洁通俗易懂;
官网链接: https://pypi.org/project/ruamel.yaml/
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : YamlDemo.py
# @CreateTime : 2019/7/24 11:07
# @Author : WeiyiGeek
# @Function : Yaml语言解析
# @Software: PyCharm
# pip install ruamel.yaml
import sys
import ruamel.yaml
def main():
#(1)读取Yaml配置文件信息
yaml = ruamel.yaml.YAML()
with open('demo.yaml','r',encoding='utf-8') as conf:
try:
config = yaml.load(conf)
except ruamel.yaml.YAMLError as e:
print("解析错误:",e)
#(2)打印出yaml的配置文件信息 常量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)修改与添加
config['NAME'] = "WeiyiGeek"
config['add'] = "WeiyiGeek"
config['obj']['age'] = "888"
config['obj']['love'][1] = "烹饪"
#(4)写入存档
with open("update.yml",'w+',encoding='utf-8') as con:
ruamel.yaml.dump(config, con, allow_unicode=True, Dumper=ruamel.yaml.RoundTripDumper) #加上Dumper原来是什么样式就是什么样式
ruamel.yaml.dump(config, sys.stdout, Dumper=ruamel.yaml.RoundTripDumper) #修改后输出到终端
if __name__ == '__main__':
main()
运行结果:
#打印结果
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写入文件结果
#常量
PI: 3.1415926
NAME: WeiyiGeek
INT: 1024
#对象(集合)
obj:
name: WeiyiGeeK
age: '888'
love: !!obj
- Computer
- "\u70F9\u996A"
- car
#数组
array:
- key1: I
- key2: Love
- key3: Study
add: WeiyiGeek
参考
描述:采用filecmp模块对进行确认备份目录与源目录文件是否保持一致,并且同步源目录文件到目标目录之中:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File : verity.py
# @CreateTime : 2019/7/26 14:23
# @Author : WeiyiGeek
# @Function : 同步校验源文件/目标文件
# @Software: PyCharm
import os,sys
import filecmp
import shutil
holderlist = []
destination = []
def compareme(src, dest):
dircomp = filecmp.dircmp(src, dest);
onlyfile = dircomp.left_only #源文件新目录或者文件
difffile = dircomp.diff_files #源目录中发生变化的文件(不匹配的文件)
dirpath = os.path.abspath(src)
#将发生变量的文件路径写入到,采用lamba表达式
[holderlist.append(os.path.abspath(os.path.join(src, x))) for x in onlyfile] #将源文件中创建的文件绝对路径存入数组中
[holderlist.append(os.path.abspath(os.path.join(src, x))) for x in difffile] #将源文件中的发生改变的文件/目录绝对路径存入数组中
#src / desc 两边都存在的文件(继续递归对比)
if len(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
if len (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) #比较源目录与备份
dir1 = os.path.abspath(src)
dir2 = os.path.abspath(dest)
createdir = False
#变量返回差异性文件 (替换路径后重新放入destination数组中为后面的zip做准备)
for item in source_files:
#获取源文件中的目录
destination_dir = item.replace(dir1,dir2)
destination.append(destination_dir)
#创建目录(再目录不存在的情况下)
if os.path.isdir(item):
if not os.path.exists(destination_dir):
os.makedirs(destination_dir)
createdir = True
#如果创建目录,重新遍历新创建的目录
if createdir:
destination = [] #防止重复
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) #源/备份目录文件清单拆分成元组
for item in copy_pair: #判断文件复制操作
if os.path.isfile(item[0]):
shutil.copyfile(item[0],item[1])
print(item)
if __name__ == '__main__':
main()
WeiyiGeek.filecmp文件目录校验