1. 程式人生 > >python之路《模塊》

python之路《模塊》

hello 之路 from 詳細信息 默認工作目錄 fse size ria fir

1.time模塊

FUNCTIONS

asctime(...)

asctime([tuple]) -> string

Convert a time tuple to a string, e.g. ‘Sat Jun 06 16:26:11 1998‘.

When the time tuple is not present, current time as returned by localtime()

is used.

clock(...)

clock() -> floating point number

Return the CPU time or real time since the start of the process or since

the first call to clock(). This has as much precision as the system

records.

ctime(...)

ctime(seconds) -> string

Convert a time in seconds since the Epoch to a string in local time.

This is equivalent to asctime(localtime(seconds)). When the time tuple is

not present, current time as returned by localtime() is used.

get_clock_info(...)

get_clock_info(name: str) -> dict

Get information of the specified clock.

gmtime(...)

gmtime([seconds]) -> (tm_year, tm_mon, tm_mday, tm_hour, tm_min,

tm_sec, tm_wday, tm_yday, tm_isdst)

Convert seconds since the Epoch to a time tuple expressing UTC (a.k.a.

GMT). When ‘seconds‘ is not passed in, convert the current time instead.

If the platform supports the tm_gmtoff and tm_zone, they are available as

attributes only.

localtime(...)

localtime([seconds]) -> (tm_year,tm_mon,tm_mday,tm_hour,tm_min,

tm_sec,tm_wday,tm_yday,tm_isdst)

Convert seconds since the Epoch to a time tuple expressing local time.

When ‘seconds‘ is not passed in, convert the current time instead.

mktime(...)

mktime(tuple) -> floating point number

Convert a time tuple in local time to seconds since the Epoch.

Note that mktime(gmtime(0)) will not generally return zero for most

time zones; instead the returned value will either be equal to that

of the timezone or altzone attributes on the time module.

monotonic(...)

monotonic() -> float

Monotonic clock, cannot go backward.

perf_counter(...)

perf_counter() -> float

Performance counter for benchmarking.

process_time(...)

process_time() -> float

Process time for profiling: sum of the kernel and user-space CPU time.

sleep(...)

sleep(seconds)

Delay execution for a given number of seconds. The argument may be

a floating point number for subsecond precision.

strftime(...)

strftime(format[, tuple]) -> string

Convert a time tuple to a string according to a format specification.

See the library reference manual for formatting codes. When the time tuple

is not present, current time as returned by localtime() is used.

Commonly used format codes:

%Y Year with century as a decimal number.

%m Month as a decimal number [01,12].

%d Day of the month as a decimal number [01,31].

%H Hour (24-hour clock) as a decimal number [00,23].

%M Minute as a decimal number [00,59].

%S Second as a decimal number [00,61].

%z Time zone offset from UTC.

%a Locale‘s abbreviated weekday name.

%A Locale‘s full weekday name.

%b Locale‘s abbreviated month name.

%B Locale‘s full month name.

%c Locale‘s appropriate date and time representation.

%I Hour (12-hour clock) as a decimal number [01,12].

%p Locale‘s equivalent of either AM or PM.

Other codes may be available on your platform. See documentation for

the C library strftime function.

strptime(...)

strptime(string, format) -> struct_time

Parse a string to a time tuple according to a format specification.

See the library reference manual for formatting codes (same as

strftime()).

Commonly used format codes:

%Y Year with century as a decimal number.

%m Month as a decimal number [01,12].

%d Day of the month as a decimal number [01,31].

%H Hour (24-hour clock) as a decimal number [00,23].

%M Minute as a decimal number [00,59].

%S Second as a decimal number [00,61].

%z Time zone offset from UTC.

%a Locale‘s abbreviated weekday name.

%A Locale‘s full weekday name.

%b Locale‘s abbreviated month name.

%B Locale‘s full month name.

%c Locale‘s appropriate date and time representation.

%I Hour (12-hour clock) as a decimal number [01,12].

%p Locale‘s equivalent of either AM or PM.

Other codes may be available on your platform. See documentation for

the C library strftime function.

time(...)

time() -> floating point number

Return the current time in seconds since the Epoch.

Fractions of a second may be present if the system clock provides them.

2.random

詳細信息

1 help(random)

我們先分析幾個常見的

# 1.隨機數0到1之間取
print(random.random())
# 2.隨機整數
print(random.randint(0, 2))
# 3.隨機整數左閉右開
print(random.randrange(0, 6))
# 隨機選值  字符穿數組元組都行
print(random.choice(hello))
# 規定長度去浮點數
print(random.uniform(0, 9))
# 洗牌功能
name = [1, 2, 3, 4, 5, 6]
random.shuffle(name)
print(name)

現在我將我們學的內容實現一個現實生活的問題

出現隨機驗證碼

check_code = ‘‘
for i in range(0, 4):     
    temp = random.randrange(0, 4)   
    if i == temp:   
        code1 = random.randint(0, 9)   # 當i與temp相同時隨機數在0到九之間
    else:
        code1 = chr(random.randint(97, 122))  # 否則就為字母
    check_code += str(code1)
print(check_code)

3.os模塊

#OS模塊 #os模塊就是對操作系統進行操作,使用該模塊必須先導入模塊:
import os #getcwd() 獲取當前工作目錄(當前工作目錄默認都是當前文件所在的文件夾)
result = os.getcwd()
print(result) #chdir()改變當前工作目錄
os.chdir(‘/home/sy‘)
result = os.getcwd()
print(result) open(‘02.txt‘,‘w‘) #操作時如果書寫完整的路徑則不需要考慮默認工作目錄的問題,按照實際書寫路徑操作
open(‘/home/sy/下載/02.txt‘,‘w‘) #listdir() 獲取指定文件夾中所有內容的名稱列表
result = os.listdir(‘/home/sy‘)
print(result) #mkdir() 創建文件夾
#os.mkdir(‘girls‘)
#os.mkdir(‘boys‘,0o777) #makedirs() 遞歸創建文件夾
#os.makedirs(‘/home/sy/a/b/c/d‘) #rmdir() 刪除空目錄
#os.rmdir(‘girls‘) #removedirs 遞歸刪除文件夾 必須都是空目錄
#os.removedirs(‘/home/sy/a/b/c/d‘) #rename() 文件或文件夾重命名
#os.rename(‘/home/sy/a‘,‘/home/sy/alibaba‘
#os.rename(‘02.txt‘,‘002.txt‘) #stat() 獲取文件或者文件夾的信息
#result = os.stat(‘/home/sy/PycharmProject/Python3/10.27/01.py)
#print(result) #system() 執行系統命令(危險函數)
#result = os.system(‘ls -al‘) #獲取隱藏文件
#print(result) #環境變量
‘‘‘
環境變量就是一些命令的集合
操作系統的環境變量就是操作系統在執行系統命令時搜索命令的目錄的集合
‘‘‘
#getenv() 獲取系統的環境變量
result = os.getenv(‘PATH‘)
print(result.split(‘:‘)) #putenv() 將一個目錄添加到環境變量中(臨時增加僅對當前腳本有效)
#os.putenv(‘PATH‘,‘/home/sy/下載‘)
#os.system(‘syls‘) #exit() 退出終端的命令 #os模塊中的常用值
#curdir 表示當前文件夾 .表示當前文件夾 一般情況下可以省略
print(os.curdir) #pardir 表示上一層文件夾 ..表示上一層文件夾 不可省略!
print(os.pardir) #os.mkdir(‘../../../man‘)#相對路徑 從當前目錄開始查找
#os.mkdir(‘/home/sy/man1‘)#絕對路徑 從根目錄開始查找 #name 獲取代表操作系統的名稱字符串
print(os.name) #posix -> linux或者unix系統 nt -> window系統 #sep 獲取系統路徑間隔符號 window ->\ linux ->/
print(os.sep) #extsep 獲取文件名稱和後綴之間的間隔符號 window & linux -> .
print(os.extsep) #linesep 獲取操作系統的換行符號 window -> \r\n linux/unix -> \n
print(repr(os.linesep)) #導入os模塊
import os #以下內容都是os.path子模塊中的內容 #abspath() 將相對路徑轉化為絕對路徑
path = ‘./boys‘#相對
result = os.path.abspath(path)
print(result) #dirname() 獲取完整路徑當中的目錄部分 & basename()獲取完整路徑當中的主體部分
path = ‘/home/sy/boys‘
result = os.path.dirname(path)
print(result) result = os.path.basename(path)
print(result) #split() 將一個完整的路徑切割成目錄部分和主體部分
path = ‘/home/sy/boys‘
result = os.path.split(path)
print(result) #join() 將2個路徑合並成一個
var1 = ‘/home/sy‘
var2 = ‘000.py‘
result = os.path.join(var1,var2)
print(result) #splitext() 將一個路徑切割成文件後綴和其他兩個部分,主要用於獲取文件的後綴
path = ‘/home/sy/000.py‘
result = os.path.splitext(path)
print(result) #getsize() 獲取文件的大小
#path = ‘/home/sy/000.py‘
#result = os.path.getsize(path)
#print(result) #isfile() 檢測是否是文件
path = ‘/home/sy/000.py‘
result = os.path.isfile(path)
print(result) #isdir() 檢測是否是文件夾
result = os.path.isdir(path)
print(result) #islink() 檢測是否是鏈接
path = ‘/initrd.img.old‘
result = os.path.islink(path)
print(result) #getctime() 獲取文件的創建時間 get create time
#getmtime() 獲取文件的修改時間 get modify time
#getatime() 獲取文件的訪問時間 get active time import time filepath = ‘/home/sy/下載/chls‘ result = os.path.getctime(filepath)
print(time.ctime(result)) result = os.path.getmtime(filepath)
print(time.ctime(result)) result = os.path.getatime(filepath)
print(time.ctime(result)) #exists() 檢測某個路徑是否真實存在
filepath = ‘/home/sy/下載/chls‘
result = os.path.exists(filepath)
print(result) #isabs() 檢測一個路徑是否是絕對路徑
path = ‘/boys‘
result = os.path.isabs(path)
print(result) #samefile() 檢測2個路徑是否是同一個文件
path1 = ‘/home/sy/下載/001‘
path2 = ‘../../../下載/001‘
result = os.path.samefile(path1,path2)
print(result)
#os.environ 用於獲取和設置系統環境變量的內置值
import os
#獲取系統環境變量 getenv() 效果
print(os.environ[‘PATH‘]) #設置系統環境變量 putenv()
os.environ[‘PATH‘] += ‘:/home/sy/下載‘
os.system(‘chls‘) 4.sys模塊 5.shutil 拷貝文件用的 壓縮也可以

python之路《模塊》