模块

    xiaoxiao2021-04-13  30

    import os,sys,shutil,zipfile,shelve ##os文件模块 print(os.environ['os']) os.system('dir') #执行系统命令 a=os.popen('dir').read() #执行dir命令 当做临时文件,然后read() print(a,'\n-------------------------') print(os.stat('info')) #文件的信息 print(os.path.abspath(__file__)) #文件的绝对路径 print(os.path.isfile('info')) #是否是文件,或者isdir目录 #sys模块 print(sys.argv) #当前文件 print(sys.stdout.write('please:')) print(sys.platform) #系统 ##shutil 高级文件,压缩,处理 shutil.copyfile('info','test') # shutil.make_archive('../test','zip','../day4') #压缩 # 解压 z=zipfile.ZipFile('../test.zip','r') z.extractall('../test/') z.close()

    shelve模块

    import shelve #shelve pickle再次封装 s=shelve.open('shelve_test') name=['laowang','21',22] s['name']=name s['b']=[1,23] s.close() a=shelve.open('shelve_test') b=a.get('name') print(a.get('name')) print(a.get('b'))

    xml模块

    import xml.etree.ElementTree as ET #xml模块 tree = ET.parse('test.xml') root = tree.getroot() print(root.tag) # 根节点的名字 # for child in root: print(child.tag, child.attrib) for i in child: print('---->', 'tag:', i.tag, '\ntext:', i.text, '\nattrib:', i.attrib) print('\n-------------------\n') # 修改 for node in root.iter('year'): print(node.tag, node.text) new_year = int(node.text) + 1 # 年份加1 node.text = str(new_year) # year节点添加属性 node.set('update', 'yes') tree.write('xmlchange.xml') # 删除 for country in root.findall('country'): rank = int(country.find('rank').text) if rank > 50: root.remove(country) tree.write('xmldel.xml') # 生成 create_xml = ET.Element('namelist') name = ET.SubElement(create_xml, 'name', attrib={'enrolled': 'yes'}) age = ET.SubElement(name, 'age', attrib={'checked': 'no'}) sex = ET.SubElement(name, 'sex') age.text = '29' sex.text = 'male' name = ET.SubElement(create_xml,'name',attrib={'enrolled':'no'}) age=ET.SubElement(name,'age') age.text='33' et=ET.ElementTree(create_xml) et.write('create_xml.xml',encoding='utf-8',xml_declaration=True) <?xml version="1.0" encoding="utf-8" ?> <alex> <country name="guojia1"> <rank updated="yes">2</rank> <year>2008</year> <neighbor name="neighbor1" direction="E"/> <neighbor name="neighbor2" direction="R"/> </country> <country name="guojia2"> <rank updated="yes">200</rank> <year>2008</year> <neighbor name="guojia2_neighbor1" direction="c"/> <neighbor name="guojia2_neighbor2" direction="w"/> </country> </alex> #md5模块 import hashlib md5 = hashlib.md5() md5.update(b'123456') print(md5.hexdigest())
    转载请注明原文地址: https://ju.6miu.com/read-668662.html

    最新回复(0)