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

熱線電話:13121318867

登錄
首頁精彩閱讀Python文件管理 (部分os包,shutil包)
Python文件管理 (部分os包,shutil包)
2017-04-17
收藏

Python文件管理 (部分os包,shutil包)

在操作系統(tǒng)下,用戶可以通過操作系統(tǒng)的命令來管理文件,參考linux文件管理相關(guān)命令。Python標(biāo)準(zhǔn)庫則允許我們從Python內(nèi)部管理文件。相同的目的,我們有了兩條途徑。盡管在Python調(diào)用標(biāo)準(zhǔn)庫的方式不如操作系統(tǒng)命令直接,但有它自己的優(yōu)勢(shì)。你可以利用Python語言,并發(fā)揮其他Python工具,形成組合的文件管理功能。Python or Shell? 這是留給用戶的選擇。本文中會(huì)盡量將兩者相似的功能相對(duì)應(yīng)。

本文基于linux文件管理背景知識(shí)

os包
os包包括各種各樣的函數(shù),以實(shí)現(xiàn)操作系統(tǒng)的許多功能。這個(gè)包非常龐雜。os包的一些命令就是用于文件管理。我們這里列出最常用的:
mkdir(path)
創(chuàng)建新目錄,path為一個(gè)字符串,表示新目錄的路徑。相當(dāng)于$mkdir命令
rmdir(path)
刪除空的目錄,path為一個(gè)字符串,表示想要?jiǎng)h除的目錄的路徑。相當(dāng)于$rmdir命令
listdir(path)
返回目錄中所有文件。相當(dāng)于$ls命令。
remove(path)
刪除path指向的文件。
rename(src, dst)
重命名文件,src和dst為兩個(gè)路徑,分別表示重命名之前和之后的路徑。
chmod(path, mode)
改變path指向的文件的權(quán)限。相當(dāng)于$chmod命令。
chown(path, uid, gid)
改變path所指向文件的擁有者和擁有組。相當(dāng)于$chown命令。
stat(path)
查看path所指向文件的附加信息,相當(dāng)于$ls -l命令。
symlink(src, dst)
為文件dst創(chuàng)建軟鏈接,src為軟鏈接文件的路徑。相當(dāng)于$ln -s命令。
getcwd()
查詢當(dāng)前工作路徑 (cwd, current working directory),相當(dāng)于$pwd命令。
比如說我們要新建目錄new:
import os
os.mkdir('/home/vamei/new')
shutil包
copy(src, dst)
復(fù)制文件,從src到dst。相當(dāng)于$cp命令。
move(src, dst)
移動(dòng)文件,從src到dst。相當(dāng)于$mv命令。
比如我們想復(fù)制文件a.txt:
import shutil
shutil.copy('a.txt', 'b.txt')
想深入細(xì)節(jié),請(qǐng)參照官方文檔os, shutil。
結(jié)合本章以及之前的內(nèi)容,我們把Python打造成一個(gè)文件管理的利器了。
總結(jié)
os包: rmdir, mkdir, listdir, remove, rename, chmod, chown, stat, symlink
shutil包: copy, move

數(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); }