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

熱線電話:13121318867

登錄
首頁精彩閱讀Python核心模塊詳解解之os模塊_python os模塊詳解
Python核心模塊詳解解之os模塊_python os模塊詳解
2016-12-29
收藏

Python核心模塊詳解解之os模塊_python os模塊詳解

Python核心模塊詳解解之os模塊
os模塊提供了多數(shù)操作系統(tǒng)的功能接口函數(shù).當os模塊被導(dǎo)入后,它會自適應(yīng)于不同的操作系統(tǒng)平臺,如posix或NT系統(tǒng)平臺,os模塊會根據(jù)不同的平臺進行相應(yīng)的操作.本節(jié)內(nèi)容將對os模塊提供的函數(shù)進行詳細的解讀.

1.1  文件操作函數(shù)

1.1.1  open()函數(shù)提供創(chuàng)建、打開、修改文件的功能。

Example 1-1. Using the os Module to Rename and Remove Files

#Filename: os-example-1.py
import os
import string
def replace(file, search_for, replace_with):
   # replace strings in a text file
   back = os.path.splitext(file)[0] + “.bak”
   temp = os.path.splitext(file)[0] + “.tmp”
   try:
       # remove old temp file, if any
       os.remove(temp)
   except os.error:
       pass
   fi = open(file)
   fo = open(temp, “w”)
   for s in fi.readlines():
       fo.write(string.replace(s, search_for, replace_with))
   fi.close()
   fo.close()
   try:
       # remove old backup file, if any
       os.remove(back)
   except os.error:
       pass
   # rename original to backup…
   os.rename(file, back)
   # …and temporary to original
   os.rename(temp, file)
# try it out!
file = “samples/sample.txt”
replace(file, “hello”, “tjena”)
replace(file, “tjena”, “hello”)

1.1.2  rename()和remove()函數(shù),對文件進行重命名和刪除操作.請參照例1-1

1.2  目錄操作

1.2.1  listdir()函數(shù),返回指定目錄下所有文件名,并保存于一列表中.但當前目錄標記(.)和父目錄標記(..)不在其中.

Example 1-2. Using the os Module to List the Files in a Directory

File: os-example-2.py
import os
for file in os.listdir(“samples”):
   print file

1.2.2  getcwd()和chdir()函數(shù),功能是獲取和改變當前工作目錄.

Example 1-3. Using the os Module to Change the Working Directory

#Filename: os-example-3.py
import os
# where are we?
cwd = os.getcwd()
print “1”, cwd
# go down
os.chdir(“samples”)
print “2”, os.getcwd()
# go back up
os.chdir(os.pardir)
print “3”, os.getcwd()

1.2.3  mkdir(),rmdir(),makedirs()和removedirs()函數(shù)用于創(chuàng)建和刪除目錄操作.mkdir,rmdir和makedirs,removedir的不同在于前者只創(chuàng)建和刪除一級目錄,而后者則能創(chuàng)建和刪除多級目錄.要刪除非空目錄則要用到shutil模塊中的rmtree()函數(shù),在shutil模塊詳解中有介紹.

Example 1-4. Create and Remove Multiple Directory Levels

#Filename: os-example-4.py
import os
os.makedirs(“test/multiple/levels”)
fp = open(“test/multiple/levels/file”, “w”)
fp.write(“inspector praline”)
fp.close()
# remove the file
os.remove(“test/multiple/levels/file”)
# and all empty directories above it
os.removedirs(“test/multiple/levels”)

1.3 文件屬性操作

1.3.1stat()函數(shù)返回一個文件的所有屬性,所有的屬性都包含在一個元組中.而fstat()函數(shù)則返回一個已經(jīng)打開的文件的所有屬性.

Example 1-32. Get Information About a File數(shù)據(jù)分析培訓(xùn)

File: os-example-1.py
import os
import time
file = “samples/sample.jpg”
def dump(st):
   mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime = st
   print “- size:”, size, “bytes”
   print “- owner:”, uid, gid
   print “- created:”, time.ctime(ctime)
   print “- last accessed:”, time.ctime(atime)
   print “- last modified:”, time.ctime(mtime)
   print “- mode:”, oct(mode)
   print “- inode/dev:”, ino, dev
# get stats for a filename
st = os.stat(file)
print “station”, file
dump(st)
print st

# get stats for an open file
fp = open(file)
st = os.fstat(fp.fileno())
print “fstat”, file
dump(st)


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

若不方便掃碼,搜微信號: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(), // 加隨機數(shù)防止緩存 type: "get", dataType: "json", success: function (data) { $('#text').hide(); $('#wait').show(); // 調(diào)用 initGeetest 進行初始化 // 參數(shù)1:配置參數(shù) // 參數(shù)2:回調(diào),回調(diào)的第一個參數(shù)驗證碼對象,之后可以使用它調(diào)用相應(yīng)的接口 initGeetest({ // 以下 4 個配置參數(shù)為必須,不能缺少 gt: data.gt, challenge: data.challenge, offline: !data.success, // 表示用戶后臺檢測極驗服務(wù)器是否宕機 new_captcha: data.new_captcha, // 用于宕機時表示是新驗證碼的宕機 product: "float", // 產(chǎn)品形式,包括:float,popup width: "280px", https: true // 更多配置參數(shù)說明請參見:http://docs.geetest.com/install/client/web-front/ }, handler); } }); } function codeCutdown() { if(_wait == 0){ //倒計時完成 $(".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 = '請輸入'+oInput.attr('placeholder')+'!'; var errTxt = '請輸入正確的'+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); }