子產品(Module)
Python 子產品(Module),是一個 Python 檔案,以 .py 結尾,包含了 Python 對象定義和Python語句。子產品讓你能夠有邏輯地組織你的 Python 代碼段。把相關的代碼配置設定到一個子產品裡能讓你的代碼更好用,更易懂。子產品能定義函數,類和變量,子產品裡也能包含可執行的代碼。
import
下例是個簡單的子產品 support.py:
#!/usr/bin/python
#-*-coding:UTF-8-*-
def print_func( par ):
print "Hello : ", par
return
在test.py導入子產品 support.py,需要把指令放在腳本的頂端:
#!/usr/bin/python
#-*-coding:UTF--*-
import support
support.print_func("test_import");
輸出:
./test.py
Hello : test_import
From…import 語句
from 語句讓你從子產品中導入一個指定的部分到目前命名空間中。文法如下:
from modname import name1[, name2[, ... nameN]]
例如,要導入子產品 fib 的 fibonacci 函數,使用如下語句:
from fib import fibonacci
From…import* 語句
把一個子產品的所有内容全都導入到目前的命名空間也是可行的,隻需使用如下聲明:
from modname import *
日期和時間
time
#!/usr/bin/python
#-*-coding:UTF-8-*-
import time;
ticks = time.time()
print "目前時間戳為:", ticks
localtime = time.localtime(time.time())
print "本地時間為 :", localtime
localtime = time.asctime( time.localtime(time.time()) )
print "本地時間為 :", localtime
# 格式化成2016-03-20 11:45:39形式
print time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
# 格式化成Sat Mar 28 22:24:24 2016形式
print time.strftime("%a %b %d %H:%M:%S %Y", time.localtime())
# 将格式字元串轉換為時間戳
a = "Sat Mar 28 22:24:24 2016"
print time.mktime(time.strptime(a,"%a %b %d %H:%M:%S %Y"))
輸出:
目前時間戳為: 1495261688.49
本地時間為 : time.struct_time(tm_year=2017, tm_mon=5, tm_mday=20, tm_hour=14, tm_min=28, tm_sec=8, tm_wday=5, tm_yday=140, tm_isdst=0)
本地時間為 : Sat May 20 14:28:08 2017
2017-05-20 14:28:08
Sat May 20 14:28:08 2017
1459175064.0
calendar
#!/usr/bin/python
#-*-coding:UTF--*-
import calendar
cal = calendar.month(, )
print "以下輸出2016年1月份的月曆:"
print cal;
輸出:
以下輸出2016年1月份的月曆:
May 2017
Mo Tu We Th Fr Sa Su
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
檔案I/O
列印到螢幕
#!/usr/bin/python
#-*-coding:UTF--*-
print "Python 是一個非常棒的語言";
你的标準螢幕上會産生以下結果:
Python 是一個非常棒的語言
讀取鍵盤輸入
raw_input函數:
#!/usr/bin/python
# -*- coding: UTF- -*-
str = raw_input("請輸入:");
print "你輸入的内容是: ", str
輸出:
請輸入:uuuu
你輸入的内容是: uuuu
input函數:
input([prompt]) 函數和 raw_input([prompt]) 函數基本類似,但是 input 可以接收一個Python表達式作為輸入,并将運算結果傳回。
#!/usr/bin/python
# -*- coding: UTF- -*-
str = input("請輸入:");
print "你輸入的内容是: ", str
輸出:
請輸入:22
你輸入的内容是: 22
打開和關閉檔案
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打開一個檔案
fo = open("foo.txt", "wb")
print "檔案名: ", fo.name
print "是否已關閉 : ", fo.closed
print "通路模式 : ", fo.mode
print "末尾是否強制加空格 : ", fo.softspace
輸出:
檔案名: foo.txt
是否已關閉 : False
通路模式 : wb
末尾是否強制加空格 :
write()方法
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打開一個檔案
fo = open("foo.txt", "wb")
fo.write( "www.runoob.com!\nVery good site!\n");
# 關閉打開的檔案
fo.close()
more foo.txt
www.runoob.com!
Very good site!
read()方法
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打開一個檔案
fo = open("foo.txt", "r+")
str = fo.read();
print "讀取的字元串是 : ", str
# 關閉打開的檔案
fo.close()
檔案定位
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 打開一個檔案
fo = open("foo.txt", "r+")
str = fo.read();
print "讀取的字元串是 : ", str
# 查找目前位置
position = fo.tell();
print "目前檔案位置 : ", position
# 把指針再次重新定位到檔案開頭
position = fo.seek(, );
str = fo.read();
print "重新讀取字元串 : ", str
# 關閉打開的檔案
fo.close()
輸出:
讀取的字元串是 : www.runoob
目前檔案位置 :
重新讀取字元串 : www.runoob
重命名和删除檔案
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
# 重命名檔案test1.txt到test2.txt。
os.rename( "test1.txt", "test2.txt" )
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
# 删除一個已經存在的檔案test2.txt
os.remove("test2.txt")
mkdir()方法
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
# 建立目錄test
os.mkdir("test")
chdir()方法
可以用chdir()方法來改變目前的目錄。chdir()方法需要的一個參數是你想設成目前目錄的目錄名稱。
#!/usr/bin/python
# -*- coding: UTF- -*-
import os
# 将目前目錄改為"/home/newdir"
os.chdir("/home/newdir")
getcwd()方法
getcwd()方法顯示目前的工作目錄
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
# 給出目前的目錄
print os.getcwd()
rmdir()方法
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import os
# 删除”/tmp/test”目錄
os.rmdir( "/tmp/test" )
1.python學習網站
http://www.runoob.com/python/python-tutorial.html