99999久久久久久亚洲,欧美人与禽猛交狂配,高清日韩av在线影院,一个人在线高清免费观看,啦啦啦在线视频免费观看www

熱線電話:13121318867

登錄
首頁(yè)大數(shù)據(jù)時(shí)代python之shutil模塊11個(gè)常用函數(shù)詳解
python之shutil模塊11個(gè)常用函數(shù)詳解
2020-07-23
收藏

最近可是被python給刷屏了,到處都是python的廣告,推廣,這也讓越來越多的人開始學(xué)python。想要學(xué)習(xí)和使用python,必須對(duì)python的基礎(chǔ)知識(shí)有一個(gè)比較全面的了解。小編今天跟大家分享的這篇文章就是關(guān)于python的shutil模塊11個(gè)常用函數(shù)的講解,希望對(duì)大家有所幫助。

以下文章來源: AI入門學(xué)習(xí)

作者: 小伍哥

shutil 是 Python 中的高級(jí)文件操作模塊,與os模塊形成互補(bǔ)的關(guān)系,os主要提供了文件或文件夾的新建、刪除、查看等方法,還提供了對(duì)文件以及目錄的路徑操作。shutil模塊提供了移動(dòng)、復(fù)制、 壓縮、解壓等操作,恰好與os互補(bǔ),共同一起使用,基本能完成所有文件的操作。是一個(gè)非常重要的模塊。

 

#加載包
import shutil
#查看包中的所有方法
print(dir(shutil))
[ 'chown', 'collections', 'copy', 'copy2', 'copyfile', 'copyfileobj', 'copymode', 'copystat', 'copytree', 'disk_usage', 'errno', 'fnmatch', 'get_archive_formats', 'get_terminal_size', 'get_unpack_formats', 'getgrnam', 'getpwnam', 'ignore_patterns', 'make_archive', 'move', 'nt', 'os', 'register_archive_format', 'register_unpack_format', 'rmtree', 'stat', 'sys', 'unpack_archive', 'unregister_archive_format', 'unregister_unpack_format', 'which']
 

 

01、copy()

描述:復(fù)制文件

語法:shutil.copy(fsrc,path),返回值:返回復(fù)制之后的路徑

  • fsrc:源文件
  • path:目標(biāo)地址
shutil.copy('test.csv','C:/Users/zhengxiang.wzx/Desktop/')
'C:/Users/zhengxiang.wzx/Desktop/test.csv'

 

02、copy2()

描述:復(fù)制文件和狀態(tài)信息

語法:shutil.copy(fsrc,path),返回值:返回復(fù)制之后的路徑

  • fsrc:源文件
  • path:目標(biāo)地址
shutil.copy2('test.csv','C:/Users/zhengxiang.wzx/Desktop/')
'C:/Users/zhengxiang.wzx/Desktop/test.csv'

 

03、copyfileobj()

描述:將一個(gè)文件的內(nèi)容拷貝到另一個(gè)文件中,如果目標(biāo)文件本身就有內(nèi)容,來源文件的內(nèi)容會(huì)把目標(biāo)文件的內(nèi)容覆蓋掉。如果文件不存在它會(huì)自動(dòng)創(chuàng)建一個(gè)。

語法:shutil.copyfileobj(fsrc, fdst[, length=16*1024]) 

  • fsrc:源文件
  • fdst:復(fù)制至fdst文件
  • length:緩沖區(qū)大小,即fsrc每次讀取的長(zhǎng)度
import shutil
f1 = open('file.txt','r')
f2 = open('file_copy.txt','w+')
shutil.copyfileobj(f1,f2,length=16*1024)

 

04、copyfile()

描述:將一個(gè)文件的內(nèi)容拷貝到另一個(gè)文件中,目標(biāo)文件無需存在

語法:shutil.copyfile(src, dst,follow_symlinks)

  • src:源文件路徑
  • dst:復(fù)制至dst文件,若dst文件不存在,將會(huì)生成一個(gè)dst文件;若存在將會(huì)被覆蓋
  • follow_symlinks:設(shè)置為True時(shí),若src為軟連接,則當(dāng)成文件復(fù)制;如果設(shè)置為False,復(fù)制軟連接。默認(rèn)為True。
#file_1不存在,會(huì)產(chǎn)生一個(gè)
shutil.copyfile('file_0.csv','file_1.csv')
'file_1.csv'

#file_2存在,直接復(fù)制
shutil.copyfile('file_0.csv','file_2.csv')
'file_2.csv'

 

05、copytree()

描述:復(fù)制整個(gè)目錄文件,不需要的文件類型可以不復(fù)制

語法:shutil.copytree(oripath, despath, ignore= shutil.ignore_patterns("*.xls", "*.doc"))

參數(shù):

  • oripath : "來源路徑"
  • despath : "目標(biāo)路徑"
  • ignore : shutil.ignore_patterns() 是對(duì)內(nèi)容進(jìn)行忽略篩選,將對(duì)應(yīng)的內(nèi)容進(jìn)行忽略。 
import shutil,os
path1 = os.path.join(os.getcwd(),"kaggle")
path1
'C:\\Users\\wuzhengxiang\\Desktop\\Python知識(shí)點(diǎn)總結(jié)\\kaggle'
#bbb與ccc文件夾都可以不存在,會(huì)自動(dòng)創(chuàng)建
path2 = os.path.join(os.getcwd(),"bbb","ccc")
path2
'C:\\Users\\wuzhengxiang\\Desktop\\Python知識(shí)點(diǎn)總結(jié)\\bbb\\ccc'
# 將"abc.txt","bcd.txt"忽略,不復(fù)制
shutil.copytree(path1,path2,ignore=shutil.ignore_patterns("abc.txt","bcd.txt"))

06、copymode()

描述:拷貝權(quán)限,前提是目標(biāo)文件存在,不然會(huì)報(bào)錯(cuò)。將src文件權(quán)限復(fù)制至dst文件。文件內(nèi)容,所有者和組不受影響

語法:shutil.copymode(src,dst)

  • src:源文件路徑
  • dst:將權(quán)限復(fù)制至dst文件,dst路徑必須是真實(shí)的路徑,并且文件必須存在,否則將會(huì)報(bào)文件找不到錯(cuò)誤
  • follow_symlinks:設(shè)置為False時(shí),src, dst皆為軟連接,可以復(fù)制軟連接權(quán)限,如果設(shè)置為True,則當(dāng)成普通文件復(fù)制權(quán)限。默認(rèn)為True。Python3新增參數(shù)
shutil.copymode("file_0.csv","file_1.csv")

 

07、move()

描述:移動(dòng)文件或文件夾

語法:shutil.move(src, dst)

os.chdir('C:/Users/wuzhengxiang/Desktop/Python知識(shí)點(diǎn)總結(jié)')
os.getcwd()
shutil.move('file_1.csv', 'C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析')
'C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析\\file_1.csv'

 

08、disk_usage()

描述:查看磁盤使用信息,計(jì)算磁盤總存儲(chǔ),已用存儲(chǔ),剩余存儲(chǔ)信息。

語法:shutil.disk_usage('盤符')

返回值:元組

shutil.disk_usage('D:')
usage(total=151199412224, used=41293144064, free=109906268160)

total,總存儲(chǔ):151199412224/1024/1024/1024=140GB
used,已使用:41293144064/1024/1024/1024=38GB
free,剩余容量:109906268160/1024/1024/1024=102GB

 

09、 make_archive()

描述:壓縮打包

語法:make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0,dry_run=0, owner=None, group=None, logger=None) 

壓縮打包

  • base_name:  壓縮包的文件名,也可以是壓縮包的路徑。只是文件名時(shí),則保存至當(dāng)前目錄,否則保存至指定路徑
  • format:  壓縮或者打包格式    "zip", "tar", "bztar"or "gztar"
  • root_dir :  將哪個(gè)目錄或者文件打包(也就是源文件)
#把當(dāng)前目錄下的file_1.csv打包壓縮
shutil.make_archive('file_1.csv','gztar',root_dir='C:/Users/wuzhengxiang/Desktop/股票數(shù)據(jù)分析')
'C:\\Users\\wuzhengxiang\\Desktop\\股票數(shù)據(jù)分析\\file_1.csv.tar.gz'

 

10、 get_archive_formats()

描述: 獲取支持的壓縮文件格式。目前支持的有:tar、zip、gztar、bztar。在Python3還多支持一種格式xztar

語法:unpack_archive(filename, extract_dir=None, format=None)

  • filename:文件路徑
  • extract_dir:解壓至的文件夾路徑。文件夾可以不存在,會(huì)自動(dòng)生成
  • format:解壓格式,默認(rèn)為None,會(huì)根據(jù)擴(kuò)展名自動(dòng)選擇解壓格式
import shutil,os
zip_path = os.path.join(os.getcwd(),"file_1.csv.tar")
extract_dir = os.path.join(os.getcwd(),"aaa")
shutil.unpack_archive(zip_path, extract_dir)

 

11、rmtree()

描述:遞歸的去刪除文件

語法:shutil.rmtree(path[, ignore_errors[, onerror]])

#刪除文件夾
shutil.rmtree('C:/Users/wuzhengxiang/Desktop/Python知識(shí)點(diǎn)總結(jié)/test2')

數(shù)據(jù)分析咨詢請(qǐng)掃描二維碼

若不方便掃碼,搜微信號(hào):CDAshujufenxi

數(shù)據(jù)分析師資訊
更多

OK
客服在線
立即咨詢
客服在線
立即咨詢
') } function initGt() { var handler = function (captchaObj) { captchaObj.appendTo('#captcha'); captchaObj.onReady(function () { $("#wait").hide(); }).onSuccess(function(){ $('.getcheckcode').removeClass('dis'); $('.getcheckcode').trigger('click'); }); window.captchaObj = captchaObj; }; $('#captcha').show(); $.ajax({ url: "/login/gtstart?t=" + (new Date()).getTime(), // 加隨機(jī)數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進(jìn)行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個(gè)參數(shù)驗(yàn)證碼對(duì)象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個(gè)配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺(tái)檢測(cè)極驗(yàn)服務(wù)器是否宕機(jī) new_captcha: data.new_captcha, // 用于宕機(jī)時(shí)表示是新驗(yàn)證碼的宕機(jī) product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請(qǐng)參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計(jì)時(shí)完成 $(".getcheckcode").removeClass('dis').html("重新獲取"); }else{ $(".getcheckcode").addClass('dis').html("重新獲取("+_wait+"s)"); _wait--; setTimeout(function () { codeCutdown(); },1000); } } function inputValidate(ele,telInput) { var oInput = ele; var inputVal = oInput.val(); var oType = ele.attr('data-type'); var oEtag = $('#etag').val(); var oErr = oInput.closest('.form_box').next('.err_txt'); var empTxt = '請(qǐng)輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請(qǐng)輸入正確的'+oInput.attr('placeholder')+'!'; var pattern; if(inputVal==""){ if(!telInput){ errFun(oErr,empTxt); } return false; }else { switch (oType){ case 'login_mobile': pattern = /^1[3456789]\d{9}$/; if(inputVal.length==11) { $.ajax({ url: '/login/checkmobile', type: "post", dataType: "json", data: { mobile: inputVal, etag: oEtag, page_ur: window.location.href, page_referer: document.referrer }, success: function (data) { } }); } break; case 'login_yzm': pattern = /^\d{6}$/; break; } if(oType=='login_mobile'){ } if(!!validateFun(pattern,inputVal)){ errFun(oErr,'') if(telInput){ $('.getcheckcode').removeClass('dis'); } }else { if(!telInput) { errFun(oErr, errTxt); }else { $('.getcheckcode').addClass('dis'); } return false; } } return true; } function errFun(obj,msg) { obj.html(msg); if(msg==''){ $('.login_submit').removeClass('dis'); }else { $('.login_submit').addClass('dis'); } } function validateFun(pat,val) { return pat.test(val); }