Python之路Day6
Day6的主要内容是:
configparser模块
shutil模块
subprocess模块
处理xml的模块
1.configparser模块
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
"""
configparser 练习
"""
import configparser
# 写一个配置文件
config = configparser.ConfigParser()
config['}
config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret[' # mutates the parser
topsecret['ForwardX11'] = 'no' # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
config.write(configfile)
# 读配置文件
config = configparser.ConfigParser()
print(config.sections())
a = config.read("test.cfg")
print(a)
print(config.sections())
print("bitbucket.org" in config.sections())
print(config["bitbucket.org"]["user"])
for key in config["bitbucket.org"]:
print(key, config["bitbucket.org"][key])
# 增删改查
config = configparser.ConfigParser()
config.read("test.cfg")
sec = config.sections()
print(sec)
options = config.options("bitbucket.org")
print(options)
item_list = config.items("bitbucket.org")
print(item_list)
val = config.get("bitbucket.org", "compressionlevel")
print(val)
val = config.getint("bitbucket.org", "compressionlevel")
print(val)
# 改写
config.remove_section("bitbucket.org")
config.write(open("test2.cfg", "w"))
sec = config.has_section("bitbuckrt.org")
print(sec)
config.add_section("bitbucket.org")
sec = config.has_section("bitbuckrt.org")
print(sec)
config.write(open("test2.cfg", "w"))
config.set(")
config.write(open("test2.cfg", "w"))
config.remove_option("topsecret.server.com", "port")
config.write(open("test2.cfg", "w"))
2.shutil模块
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
"""
高级的 文件、文件夹、压缩包 处理模块
"""
import shutil
import os
# 将文件内容(文件对象)拷贝到另一个文件中,可以指定部分拷贝
# with open("D:\qimi_WorkSpace\S12\day6\\test1.txt", "rt") as f1, open("D:\qimi_WorkSpace\S12\day6\\test2.txt", "at")as f2:
# shutil.copyfileobj(fsrc=f1, fdst=f2)
# 拷贝文件
# shutil.copyfile(src="D:\qimi_WorkSpace\S12\day6\\test1.txt",dst="D:\qimi_WorkSpace\S12\day6\\test2.txt")
# 仅拷贝权限。内容、组、用户均不变
# print(os.stat("D:\qimi_WorkSpace\S12\day6\\test2.txt"))
# shutil.copymode(src="D:\qimi_WorkSpace\S12\day6\\test1.txt", dst="D:\qimi_WorkSpace\S12\day6\\test2.txt")
# print(os.stat("D:\qimi_WorkSpace\S12\day6\\test2.txt"))
# # 拷贝状态的信息,包括:mode bits, atime, mtime, flags
# shutil.copystat(src=,dst=)
#
# # 拷贝文件和权限
# shutil.copy(src, dst)
# 拷贝文件和状态信息
# shutil.copy2(src,dst)
# 递归的去拷贝文件
# shutil.ignore_patterns(*patterns)
# shutil.copytree(src, dst, symlinks=False, ignore=None)
# 递归的去删除文件
# shutil.rmtree(path[, ignore_errors[, onerror]])
# 递归的去移动文件
# shutil.move(src, dst)
# 创建压缩包并返回文件路径,例如:zip、tar
# shutil.make_archive(base_name, format,...)
#
# base_name: 压缩包的文件名,也可以是压缩包的路径。只是文件名时,则保存至当前目录,否则保存至指定路径,
# 如:www =>保存至当前路径
# 如:/Users/wupeiqi/www =>保存至/Users/wupeiqi/
# format: 压缩包种类,“zip”, “tar”, “bztar”,“gztar”
# root_dir: 要压缩的文件夹路径(默认当前目录)
# owner: 用户,默认当前用户
# group: 组,默认当前组
# logger: 用于记录日志,通常是logging.Logger对象
# 将D:\qimi_WorkSpace\S12\day6目录下的文件打包成test.tar.gz,放置在当前目录
et = shutil.make_archive("test", 'gztar', root_dir='D:\\qimi_WorkSpace\\S12\\day6')
# shutil模块对压缩包的处理是调用ZipFile和TarFile两个模块来进行的
# zipfile模块
import zipfile
# 压缩
z = zipfile.ZipFile('test.zip', 'w')
z.write('a.log')
z.write('a.data')
z.close()
# 解压
z = zipfile.ZipFile('test.zip', 'r')
z.extractall()
z.close()
# tarfile模块
import tarfile
# 压缩
tar = tarfile.open('test.tar','w')
tar.add('D:\\qimi_WorkSpace\\S12\\day6\\test1.tar', arcname='test1.tar')
tar.add('D:\\qimi_WorkSpace\\S12\\day6\\test2.tar', arcname='test2.tar')
tar.close()
# 解压
tar = tarfile.open('test.tar','r')
tar.extractall() # 可设置解压地址
tar.close()
3.subprocess模块
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
"""
subprocess模块的练习
"""
import subprocess
subprocess.run("ipconfig")
# subprocess.Popen()用于执行复杂的系统命令
p = subprocess.Popen("ifconfig", shell=True, stdout=subprocess.PIPE)
print(p.stdout.read())
# 需要交互的命令用到管道PIPE
obj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
obj.stdin.write(b"print('hello1')\n")
obj.stdin.write(b"print('hello2')\n")
obj.stdin.write(b"print('hello3')\n")
a = obj.communicate(timeout=10)
print(a)
4.处理xml文件的模块
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# __author__ = "Q1mi"
"""
xml模块的练习
"""
import xml.etree.ElementTree as ET
# 解析xml文件
tree = ET.parse("test.xml")
# 获取根
root = tree.getroot()
print(root.tag)
# 遍历xml文档
for child in root:
print(child.tag, child.attrib)
for i in child:
print(i.tag, i.text)
# 只遍历year节点
for i in root.iter("year"):
print(i.tag, i.text)
# 修改和删除xml文件
tree = ET.parse("test2.xml")
root = tree.getroot()
Python之路Day6的更多相关文章
- (转)Python之路,Day6 - 面向对象学习
本节内容: 面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法. 引子 你现在是一家游戏公司的开发人员,现在需要你开发一款叫做<人狗大战> ...
- 转:Python之路,Day6 - 面向对象学习
这篇文章写的不错,转来收了 转自:http://www.cnblogs.com/alex3714/articles/5188179.html 本节内容: 面向对象编程介绍 为什么要用面向对象进 ...
- 十一Python之路,Day6 - 面向对象学习
本节内容: 面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法. 引子 你现在是一家游戏公司的开发人员,现在需要你开发一款叫做<人狗大战& ...
- Python之路,Day6 - Python基础6
本节大纲: 模块介绍 time &datetime模块 random os sys shutil json & picle shelve xml处理 yaml处理 configpars ...
- Python之路,Day6 - 面向对象学习
本节内容: 面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法. 引子 你现在是一家游戏公司的开发人员,现在需要你开发一款叫做<人狗大战>的游戏 ...
- python之路-Day6
time & datetime模块 #_*_coding:utf-8_*_ __author__ = 'Alex Li' import time # print(time.clock()) # ...
- Python之路
Python学习之路 第一天 Python之路,Day1 - Python基础1介绍.基本语法.流程控制 第一天作业第二天 Python之路,Day2 - Pytho ...
- Python之路【第一篇】python基础
一.python开发 1.开发: 1)高级语言:python .Java .PHP. C# Go ruby c++ ===>字节码 2)低级语言:c .汇编 2.语言之间的对比: 1)py ...
- Python学习记录day6
title: Python学习记录day6 tags: python author: Chinge Yang date: 2016-12-03 --- Python学习记录day6 @(学习)[pyt ...
随机推荐
- IE 弹出提示:由于无法验证发布者,所以Windows 已经阻止此软件
由于无法验证发布者,所以Windows 已经阻止此软件 按如下步骤:1.打开Internet Explorer---菜单栏点“工具”---Internet选项--安全---自定义级别---安全设置-- ...
- python 从数据库表生成model
python 从数据库表生成model 找了很久才找到这个,我是新手... 现在已有建好的数据库,需要基于原有数据做数据分析的web应用,我选择python+Tornado ,由于不想写SQL语句,就 ...
- ruby2.0(rails)以后版本的debug
很喜欢RUBY(RAILS),认识也好久好久了,但是说实话,从来没用ROR写过什么东西,都是小打小闹,做些自娱自乐的东西,碰到什么问题,基本仔细看看,加上几个LOG就找到原因了,从来没想过要DEBUG ...
- JAVA GUI学习 - JTree树结构组件学习 ***
public class JTreeKnow extends JFrame { public JTreeKnow() { this.setBounds(300, 100, 400, 500); thi ...
- flex正则表达式
正则表达式是一种通用的标准,大部分计算机语言都支持正则表达式,包括as3,这里收集了一些常用的正则表达式语句,大家用到的时候就不用自己写了 ^\d+$ //匹配非负整数(正整数 + 0) ^[0-9] ...
- struts2自己定义类型转换器
1.1. struts2自己定义类型转换器 1) 自定类型转换类,继承DefaultTypeConverter类 package com.morris.ticket.conversio ...
- BZOJ2440(全然平方数)二分+莫比乌斯容斥
题意:全然平方数是指含有平方数因子的数.求第ki个非全然平方数. 解法:比較明显的二分,getsum(int middle)求1-middle有多少个非全然平方数,然后二分.求1-middle的非全然 ...
- C#语言基础之数据类型
数据类型 1.值类型(1)整型:有符号整型和无符号整型. 区别是无符号整型要比有符号整型的正数范围大.2X+1 有符号整型:sbyte,short,int,long 带有正负数,范围按所写依次增大 ...
- [置顶] 程序员必知(二):位图(bitmap)
位图是什么? 位图就是数组,一般来说是bit型的数组,具有快速定位某个值的功能,这种思想有很广泛的应用,比如下边两题: 1 找出一个不在5TB个整数中存在的数 假设整数是32位的,总共有4GB个数,我 ...
- jbpmAPI-1
1.1. What is jBPM? jBPM是一个灵活的业务流程管理(BPM)套件.它是轻量级的,完全开源Apache许可下(分布式),用Java编写的.它允许您模型.执行和监控业务流程的整个生命周 ...