python 重要模組

2021-09-06 18:29:59 字數 1564 閱讀 9366

1,使用字典的特殊字串替換,基於字典的字串格式化

圓括號中的資訊是鍵名,值將從字典中得到並替換為字串,需在圓括號後面指定插入的資料型別;

person[數字序 for tulpper() list or keyname for dict{}]

2,string

ruiy tips

python模組

使用模組為python新增功能,通過模組同作業系統及其檔案進行互動.

options and arguments corresponding environment variables;

program passed in as string (terminates option list)

debug output from parser (pythondebug = x)

ignore environment variables

3,使用乙個以上的程序fork,exec(執行系統呼叫)

os.fork()

告訴計算機複製關於當前執行的程式的一切資訊島乙個新建立的單獨程式,複製的程式與原來的程式幾乎是完全相同的!

import

ospid =os.fork()

if pid == 0:#

this is the child

print("

this is the child")

else

:print("

the child is pid %d

" % pid)

os.wait()使python parent id什麼都不做;

import os

pid = os.foek()

#fork and exec together

print("second test")

if pid == 0:#this is the child

print("this is the child")

print("i'm going to exec another program now")

os.execl('/bin/cat','cat','/etc/moth')

else:

print("the child is pid %d" % pid)

os.wait()

4,使用python 萬用字元篩選有用的python模組私有及共有模組物件

import glob

如果只需要乙個新命令的最基本的呼叫,最簡單的方式是使用os.system函式

上面簡單說了下使用乙個以上的程序(程序分叉),下面來講講執行緒(在相同的程序中完成多個工作)

5,建立模組

模組提供了一種在應用程式之間共享python**的便捷方式.

模組中定義函式和類;

匯入python內建模組 or自定義的模組 import modulename;

僅僅匯入模組中的類或是函式from module import item;

import module;

import imp;

imp.reload(module);

6,修改模組搜尋路徑

python 模組搜尋路徑(重要)

當我們匯入某給模組檔案時,python直譯器去 找這個檔案,只有找到這個檔案才能讀取 裝載執行該模組檔案,一般按照如下路徑尋找模組檔案 按照順序尋找,找到即停不繼續往下找 內建模組 當前目錄 程式的主目錄 pythonpath目錄 如果已經設定,pythonpath環境變數 windows設定的系統...

Python 裝飾器與重要模組

def 裝飾器函式 原函式 pass 裝飾器函式名 語法糖 def 原函式 passimport random print dir random random.randint random.random random.choice 基礎 time import time time.time 獲取時間...

重要 python之模組CGI 通用閘道器介面

coding utf 8 python 27 xiaodeng cgi模組 import cgi 通用閘道器介面,它是一段程式,執行在伺服器上如 http伺服器,提供同客戶端html頁面的介面。請注意 cgi不是語言,他只是一種協議。通過cgi介面,web伺服器就能夠獲得客戶端提交的資訊,轉交給伺服...