Python常用知識點

來源:互聯網
上載者:User

標籤:python   os模組   

a.isdigit()
判斷是不是純數字,是 True
import string
if i in string.digits: 判斷是不是存數字

os.listdir(‘/home‘)
列出所有檔案,只有一層,以列表形式
os.listdir(‘.‘) 列出檔案

os.path.dirname(__file__)
目前的目錄,就是指令碼所在目錄

os.path.abspath(‘tmp‘)
tmp這個檔案或者檔案夾的絕對路徑

os.path.isdir(‘/home‘)
判斷是不是一個目錄,是True
os.path.isfile(‘/home‘)
判斷是不是一個檔案,是False

os.path.join(‘/home‘,‘xxx‘)
結果:/home/xxx
不會判斷是否正確,只會串連路徑

os.path.exists(self.workdir):
判斷目錄時候存在

os.mkdir(xxx)
建立目錄

os.remove()
刪除檔案

os.chdir(xxx)
進入到目錄

修改目錄許可權
os.system("chown mysql:mysql")

擷取目錄大小
os.path.getsize()

os.kill(int(pid號),15) 15是訊號
結束進程

dic.has_key(md5)
判斷md5是不是dic的key

對字典做遍曆,
for k,v in dic.items():
去掉字典分鐘的公告
for k,v in dic.items():
    dmi[k]=v.strip[]


往字典加key和value
In [5]: dic[‘a‘]=[‘b‘]
In [6]: dic
Out[6]: {‘a‘: [‘b‘]}


with open(xx,‘w’)以W開啟as fd:
    fd.write(xx)

from subprocess import Popen, PIPE
引用shell命令
如:p=Popen(‘ifconfig‘,shell=True)

 from subprocess import Popen,PIPE
p = Popen([‘pidof‘,‘md‘],stdout=PIPE)
p.stdout.read().strip  去掉/n

open(‘/etc/passwd‘).read() 把所有內容顯示出來

__file__ 代表叫本身,和$0一樣

把字典轉換成列表
i for i in dict
i.strip()去除\n
i.strip().replace(‘#‘,‘‘) 把#換成空

strip去除空格
a[0].strip 如果0是空格或者分行符號,得到的是false
   如果有內容,技術true

列錶轉換成字典
>>>list1 = [‘key1‘,‘key2‘,‘key3‘]J:
>>>list2 = [‘1‘,‘2‘,‘3‘]
>>>dict(zip(list1,list2))

延時10秒輸出
import time
time.sleep(10)

計數幾行
data.count(‘\n‘)

標準錯誤輸出,就是不輸出錯誤
print >> sys.stderr, "xxx"
py 12.py 2>/dev/null

i.strip()  去掉分行符號,定位字元

去除空行
i for i in data.split(‘\n‘) if i


from subprocss import Popen,PIPE
p=Popen(‘vim‘,stdout=PIPE,stderr=PIPE)
p.communicate() 不查看結果


添加時間
start=time.time()
time.sleep(3)
end=time.time()
pring ‘‘%0.2f‘‘ %(end - start) 查看運行多少秒

append() 方法向列表的尾部添加一個新的元素







Python常用知識點

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.