模塊補充,內置函數,異常處理

1、shutil模塊

高級的 文件、文件夾、壓縮包 處理模塊html

shutil.copyfileobj(fsrc, fdst[, length])
將文件內容拷貝到另外一個文件中node

import shutil
shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))

shutil.copyfile(src, dst)
拷貝文件python

shutil.copymode('f1.log', 'f2.log') #目標文件必須存在

shutil.copy(src, dst)
拷貝文件和權限mysql

import shutil  
shutil.copy('f1.log', 'f2.log')

shutil.copy2(src, dst)
拷貝文件和狀態信息git

import shutil  
shutil.copy2('f1.log', 'f2.log')

shutil.ignore_patterns(*patterns)
shutil.copytree(src, dst, symlinks=False, ignore=None)
遞歸的去拷貝文件夾web

import shutil
  
shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) #目標目錄不能存在,#注意對folder2目錄父級目錄要有可寫權限,ignore的意思是排除 

shutil.rmtree(path[, ignore_errors[, onerror]])
遞歸的去刪除文件sql

import shutil  
shutil.rmtree('folder1')

shutil.move(src, dst)
遞歸的去移動文件,它相似mv命令,其實就是重命名。shell

import shutil  
shutil.move('folder1', 'folder3')

shutil.make_archive(base_name, format,...)json

建立壓縮包並返回文件路徑,例如:zip、tarwindows

建立壓縮包並返回文件路徑,例如:zip、tar

    • base_name: 壓縮包的文件名,也能夠是壓縮包的路徑。只是文件名時,則保存至當前目錄,不然保存至指定路徑,
      如 data_bak                       =>保存至當前路徑
      如:/tmp/data_bak =>保存至/tmp/
    • format: 壓縮包種類,「zip」, 「tar」, 「bztar」,「gztar」
    • root_dir: 要壓縮的文件夾路徑(默認當前目錄)
    • owner: 用戶,默認當前用戶
    • group: 組,默認當前組
    • logger: 用於記錄日誌,一般是logging.Logger對象
#一、將 /data 下的文件打包放置當前程序目錄
import shutil
ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
   
#二、將 /data下的文件打包放置 /tmp/目錄
import shutil
ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data') 

shutil 對壓縮包的處理是調用 ZipFile 和 TarFile 兩個模塊來進行的,詳細:

import zipfile

# 壓縮
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()

# 解壓
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall(path='.')
z.close()
zipfile壓縮解壓縮
import tarfile

# 壓縮
>>> t=tarfile.open('/tmp/egon.tar','w')
>>> t.add('/test1/a.py',arcname='a.bak')
>>> t.add('/test1/b.py',arcname='b.bak')
>>> t.close()


# 解壓
>>> t=tarfile.open('/tmp/egon.tar','r')
>>> t.extractall('/egon')
>>> t.close()
tarfile壓縮解壓縮

 

2、shelve模塊

 shelve模塊比pickle模塊簡單,只有一個open函數,返回相似字典的對象,可讀可寫;key必須爲字符串,而值能夠是python所支持的數據類型

import shelve

f=shelve.open(r'sheve.txt')
# f['stu1_info']={'name':'egon','age':18,'hobby':['piao','smoking','drinking']}
# f['stu2_info']={'name':'gangdan','age':53}
# f['school_info']={'website':'http://www.pypy.org','city':'beijing'}

print(f['stu1_info']['hobby'])
f.close()

 

3、xml模塊

xml是實現不一樣語言或程序之間進行數據交換的協議,跟json差很少,但json使用起來更簡單,不過,古時候,在json還沒誕生的黑暗年代,你們只能選擇用xml呀,至今不少傳統公司如金融行業的不少系統的接口還主要是xml。

xml的格式以下,就是經過<>節點來區別數據結構的:

<?xml version="1.0"?>
<data>
    <country name="Liechtenstein">
        <rank updated="yes">2</rank>
        <year>2008</year>
        <gdppc>141100</gdppc>
        <neighbor name="Austria" direction="E"/>
        <neighbor name="Switzerland" direction="W"/>
    </country>
    <country name="Singapore">
        <rank updated="yes">5</rank>
        <year>2011</year>
        <gdppc>59900</gdppc>
        <neighbor name="Malaysia" direction="N"/>
    </country>
    <country name="Panama">
        <rank updated="yes">69</rank>
        <year>2011</year>
        <gdppc>13600</gdppc>
        <neighbor name="Costa Rica" direction="W"/>
        <neighbor name="Colombia" direction="E"/>
    </country>
</data>
xml數據

xml協議在各個語言裏的都 是支持的,在python中能夠用如下模塊操做xml:

# print(root.iter('year')) #全文搜索
# print(root.find('country')) #在root的子節點找,只找一個
# print(root.findall('country')) #在root的子節點找,找全部
import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
print(root.tag)
 
#遍歷xml文檔
for child in root:
    print('========>',child.tag,child.attrib,child.attrib['name'])
    for i in child:
        print(i.tag,i.attrib,i.text)
 
#只遍歷year 節點
for node in root.iter('year'):
    print(node.tag,node.text)
#---------------------------------------

import xml.etree.ElementTree as ET
 
tree = ET.parse("xmltest.xml")
root = tree.getroot()
 
#修改
for node in root.iter('year'):
    new_year=int(node.text)+1
    node.text=str(new_year)
    node.set('updated','yes')
    node.set('version','1.0')
tree.write('test.xml')
 
 
#刪除node
for country in root.findall('country'):
   rank = int(country.find('rank').text)
   if rank > 50:
     root.remove(country)
 
tree.write('output.xml')
View Code
#在country內添加(append)節點year2
import xml.etree.ElementTree as ET
tree = ET.parse("a.xml")
root=tree.getroot()
for country in root.findall('country'):
    for year in country.findall('year'):
        if int(year.text) > 2000:
            year2=ET.Element('year2')
            year2.text='新年'
            year2.attrib={'update':'yes'}
            country.append(year2) #往country節點下添加子節點

tree.write('a.xml.swap')
import xml.etree.ElementTree as ET
 
 
new_xml = ET.Element("namelist")
name = ET.SubElement(new_xml,"name",attrib={"enrolled":"yes"})
age = ET.SubElement(name,"age",attrib={"checked":"no"})
sex = ET.SubElement(name,"sex")
sex.text = '33'
name2 = ET.SubElement(new_xml,"name",attrib={"enrolled":"no"})
age = ET.SubElement(name2,"age")
age.text = '19'
 
et = ET.ElementTree(new_xml) #生成文檔對象
et.write("test.xml", encoding="utf-8",xml_declaration=True)
 
ET.dump(new_xml) #打印生成的格式
生成xml文檔方式

 

4、configparser模塊

[mogu] #section
name="mogu"  #k , v 的形式
password="123"
salary=3.1
is_cool=True

[xiaoming] #section
name="xiaoming"
password="123456"
salary=3.2
is_sb=True
user.ini(ini數據格式)
import configparser

config=configparser.ConfigParser()
config.read('user.ini')

#查看全部的標題
res=config.sections() #['mogu', 'xiaoming']
print(res)

#查看標題'mogu'下全部key=value的key
options=config.options('mogu')
print(options) # ['name', 'password', 'salary', 'is_cool']

#查看標題'mogu'下全部key=value的(key,value)格式
item_list=config.items('mogu')
print(item_list) #[('name', '"mogu"'), ('password', '"123"'), ('salary', '3.1'), ('is_cool', 'True')]

#查看標題section1下user的值=>字符串格式
val=config.get('mogu','salary')
print(val) # '3.1'

#查看標題section1下age的值=>整數格式
val1=config.getint('mogu','password')
print(val1) #123

#查看標題section1下is_admin的值=>布爾值格式
val2=config.getboolean('mogu','is_cool')
print(val2) #True

#查看標題section1下salary的值=>浮點型格式
val3=config.getfloat('mogu','salary')
print(val3) #3.1

改寫

#刪除整個標題section2
config.remove_section('xiaoming')

#刪除標題'mogu'下的某個k1和k2
config.remove_option('mogu','name')
config.remove_option('mogu','salary')

#判斷是否存在某個標題
print(config.has_section('mogu'))

#判斷標題'mogu'下是否有user
print(config.has_option('mogu',''))


#添加一個標題
config.add_section('zhangsan')

#在標題zhangsan下添加name=xiaosan,age=58的配置
config.set('zhangsan','name','xiaosan')
config.set('zhangsan','age','58') #報錯,必須是字符串


#最後將修改的內容寫入文件,完成最終的修改
config.write(open('user.ini','w'))
import configparser

config = configparser.ConfigParser()
config["DEFAULT"] = {'ServerAliveInterval': '45',
                     'Compression': 'yes',
                     'CompressionLevel': '9'}

config['bitbucket.org'] = {}
config['bitbucket.org']['User'] = 'hg'
config['topsecret.server.com'] = {}
topsecret = config['topsecret.server.com']
topsecret['Host Port'] = '50022'  # mutates the parser
topsecret['ForwardX11'] = 'no'  # same here
config['DEFAULT']['ForwardX11'] = 'yes'
with open('example.ini', 'w') as configfile:
    config.write(configfile)
基於上述建立一個ini配置文檔

 

5、subprocess模塊

import subprocess
cmd=input('輸入命令吧:').strip()
obj=subprocess.Popen(
    '%s'%cmd,
    shell=True,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

res=obj.stdout.read()
print(res.decode('gbk'))

print('='*30)
res1=obj.stderr.read()
print(res1.decode('gbk'))
 1 import  subprocess
 2 
 3 '''
 4 sh-3.2# ls /Users/egon/Desktop |grep txt$
 5 mysql.txt
 6 tt.txt
 7 事物.txt
 8 '''
 9 
10 res1=subprocess.Popen('ls /Users/jieli/Desktop',shell=True,stdout=subprocess.PIPE)
11 res=subprocess.Popen('grep txt$',shell=True,stdin=res1.stdout,
12                  stdout=subprocess.PIPE)
13 
14 print(res.stdout.read().decode('utf-8'))
15 
16 
17 #等同於上面,可是上面的優點在於,一個數據流能夠和另一個數據流交互,能夠經過爬蟲獲得結果真後交給grep
18 res1=subprocess.Popen('ls /Users/jieli/Desktop |grep txt$',shell=True,stdout=subprocess.PIPE)
19 print(res1.stdout.read().decode('utf-8'))
20 
21 
22 #windows下:
23 # dir | findstr 'test*'
24 # dir | findstr 'txt$'
25 import subprocess
26 res1=subprocess.Popen(r'dir C:\Users\Administrator\PycharmProjects\test\函數備課',shell=True,stdout=subprocess.PIPE)
27 res=subprocess.Popen('findstr test*',shell=True,stdin=res1.stdout,
28                  stdout=subprocess.PIPE)
29 
30 print(res.stdout.read().decode('gbk')) #subprocess使用當前系統默認編碼,獲得結果爲bytes類型,在windows下須要用gbk解碼
View Code

 

6、內置函數

上述圖片看不到的部分可在新標籤頁打開圖片 

詳細請參考:http://www.runoob.com/python3/python3-built-in-functions.html

 

7、異常處理

1、什麼是異常

異常就是程序運行時發生錯誤的信號(在程序出現錯誤時,則會產生一個異常,若程序沒有處理它,則會拋出該異常,程序的運行也隨之終止),在python中,錯誤觸發的異常以下

而錯誤分紅兩種

#語法錯誤示範一
if
#語法錯誤示範二
def test:
    pass
#語法錯誤示範三
class Foo
    pass
#語法錯誤示範四
print(haha
1.語法錯誤(這種錯誤,根本過不了python解釋器的語法檢測,必須在程序執行前就改正)
#TypeError:int類型不可迭代
for i in 3:
    pass
#ValueError
num=input(">>: ") #輸入hello
int(num)
#NameError
aaa
#IndexError
l=['bb','aa']
l[3]
#KeyError
dic={'name':'mogu'}
dic['age']
# AttributeError
class Foo:pass
Foo.x
#ZeroDivisionError:沒法完成計算
res1=1/0
res2=1+'str'
2.邏輯錯誤

 

2、異常的種類

在python中不一樣的異常能夠用不一樣的類型(python中統一了類與類型,類型即類)去標識,一個異常標識一種錯誤

AttributeError   # 試圖訪問一個對象沒有的樹形,好比foo.x,可是foo沒有屬性x
IOError          # 輸入/輸出異常;基本上是沒法打開文件
ImportError      # 沒法引入模塊或包;基本上是路徑問題或名稱錯誤
IndentationError # 語法錯誤(的子類) ;代碼沒有正確對齊
IndexError       # 下標索引超出序列邊界,好比當x只有三個元素,卻試圖訪問x[5]
KeyError         # 試圖訪問字典裏不存在的鍵
KeyboardInterrupt# Ctrl+C被按下
NameError        # 使用一個還未被賦予對象的變量
SyntaxError      # Python代碼非法,代碼不能編譯(我的認爲這是語法錯誤,寫錯了)
TypeError        # 傳入對象類型與要求的不符合
UnboundLocalError# 試圖訪問一個還未被設置的局部變量,基本上是因爲另有一個同名的全局變量,致使你覺得正在訪問它
ValueError       # 傳入一個調用者不指望的值,即便值的類型是正確的
經常使用異常
ArithmeticError
AssertionError
AttributeError
BaseException
BufferError
BytesWarning
DeprecationWarning
EnvironmentError
EOFError
Exception
FloatingPointError
FutureWarning
GeneratorExit
ImportError
ImportWarning
IndentationError
IndexError
IOError
KeyboardInterrupt
KeyError
LookupError
MemoryError
NameError
NotImplementedError
OSError
OverflowError
PendingDeprecationWarning
ReferenceError
RuntimeError
RuntimeWarning
StandardError
StopIteration
SyntaxError
SyntaxWarning
SystemError
SystemExit
TabError
TypeError
UnboundLocalError
UnicodeDecodeError
UnicodeEncodeError
UnicodeError
UnicodeTranslateError
UnicodeWarning
UserWarning
ValueError
Warning
ZeroDivisionError
更多異常

 

3、異常處理

爲了保證程序的健壯性與容錯性,即在遇到錯誤時程序不會崩潰,咱們須要對異常進行處理,

若是錯誤發生的條件是可預知的,咱們須要用if進行處理:在錯誤發生以前進行預防

AGE=10
while True:
    age=input('>>: ').strip()
    if age.isdigit(): #只有在age爲字符串形式的整數時,下列代碼纔不會出錯,該條件是可預知的
        age=int(age)
        if age == AGE:
            print('you got it')
            break
if 規避異常

 

若是錯誤發生的條件是不可預知的,則須要用到try...except:在錯誤發生以後進行處理

#基本語法爲
try:
    被檢測的代碼塊
except 異常類型:
    try中一旦檢測到異常,就執行這個位置的邏輯
#舉例
try:
    f=open('a.txt')
    g=(line.strip() for line in f)
    print(next(g))
    print(next(g))
    print(next(g))
    print(next(g))
    print(next(g))
except StopIteration:
    f.close()

更多用法:

#1 異常類只能用來處理指定的異常狀況,若是非指定異常則沒法處理。
s1 = 'hello'
try:
    int(s1)
except IndexError as e: # 未捕獲到異常,程序直接報錯
    print e

#2 多分支
s1 = 'hello'
try:
    int(s1)
except IndexError as e:
    print(e)
except KeyError as e:
    print(e)
except ValueError as e:
    print(e)

#3 萬能異常Exception
s1 = 'hello'
try:
    int(s1)
except Exception as e:
    print(e)

#4 多分支異常與萬能異常
#4.1 若是你想要的效果是,不管出現什麼異常,咱們統一丟棄,或者使用同一段代碼邏輯去處理他們,那麼騷年,大膽的去作吧,只有一個Exception就足夠了。
#4.2 若是你想要的效果是,對於不一樣的異常咱們須要定製不一樣的處理邏輯,那就須要用到多分支了。

#5 也能夠在多分支後來一個Exception
s1 = 'hello'
try:
    int(s1)
except IndexError as e:
    print(e)
except KeyError as e:
    print(e)
except ValueError as e:
    print(e)
except Exception as e:
    print(e)

#6 異常的其餘機構
s1 = 'hello'
try:
    int(s1)
except IndexError as e:
    print(e)
except KeyError as e:
    print(e)
except ValueError as e:
    print(e)
#except Exception as e:
#    print(e)
else:
    print('try內代碼塊沒有異常則執行我')
finally:
    print('不管異常與否,都會執行該模塊,一般是進行清理工做')

#7 主動觸發異常
try:
    raise TypeError('類型錯誤')
except Exception as e:
    print(e)

#8 自定義異常
class EgonException(BaseException):
    def __init__(self,msg):
        self.msg=msg
    def __str__(self):
        return self.msg

try:
    raise EgonException('類型錯誤')
except EgonException as e:
    print(e)

#9 斷言:assert 條件
assert 1 == 1  
assert 1 == 2

#10 總結try..except

1:把錯誤處理和真正的工做分開來
2:代碼更易組織,更清晰,複雜的工做任務更容易實現;
3:毫無疑問,更安全了,不至於因爲一些小的疏忽而使程序意外崩潰了;

 

4、何時用異常處理

首先try...except是你附加給你的程序的一種異常處理的邏輯,與你的主要的工做是沒有關係的,這種東西加的多了,會致使你的代碼可讀性變差。

只有在錯誤發生的條件沒法預知的狀況下,才應該加上try...except

相關文章
相關標籤/搜索