模块
文件是python在物理上组织代码的形式 模块是python在逻辑上组织代码的形式 每一个以.py结尾的python文件就是一个模块。将.py扩展名移除后的文件名就是模块名
1)创建模块
[ root@localhost day03] vim star. py
hi = 'Hello World!'
def pstar ( n= 30 ) :
print ( '*' * n)
[ root@localhost day03] python3
>> > import star
>> > star. hi
'Hello World!'
>> > star. pstar( )
** ** ** ** ** ** ** ** ** ** ** ** ** ** **
[ root@localhost day03] vim a. py
import star
print ( star. hi)
star. pstar( 50 )
[ root@localhost day03] python3 a. py
Hello World!
** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** ** **
2)导入模块的方法
>> > import sys
>> > from random import randint, choice
>> > randint( 1 , 100 )
3
>> > choice( 'abcd' )
'c'
>> > import time, os, string
>> > import pickle as p
3)加载(load)
一个模块只被加载一次,无论它被入多少次 只加载一次可以阻止多重导入时代码被多次执行 如果两个文件相互导入,防止了无限的相互加载 模块加载时,顶层代码会自动执行,所以只将函数放入模块的顶层是良好的编程习惯
>> > import this
>> > import this
4)模块导入的特性
需求:某些代码在程序文件直接运行时执行;当程序文件被当作模块导入时不执行 特特殊属性__name__
它是一个变量,它有两种值
当程序文件直接运行时,它的值是__main__
当程序文件被当作模块导入时,它的值是模块名
[ root@localhost day03]
print ( __name__)
[ root@localhost day03]
import foo
[ root@localhost day03]
__main__
[ root@localhost day03]
foo
if __name__ == '__main__' :
print ( 'aaaaa' )
random
>> > import random
>> > random. choice( 'abc' )
'a'
>> > random. choice( 'abc' )
>> > random. choice( ( 'aaaa' , 'bbb' , 'ccc' ) )
'bbb'
>> > random. choice( ( 'aaaa' , 'bbb' , 'ccc' ) )
'bbb'
>> > random. choice( ( 'aaaa' , 'bbb' , 'ccc' ) )
'aaaa'
>> > random. randint( 1 , 100 )
53
>> > random. randint( 1 , 100 )
12
shutil
import shutil
>> > shutil. copy( '/etc/hosts' , '/tmp/zj2.txt' )
>> > shutil. move( '/tmp/zj.txt' , '/var/tmp' )
>> > shutil. copytree( '/etc/security' , '/tmp/anquan' )
>> > shutil. rmtree( '/tmp/anquan' )
>> > shutil. chown( '/tmp/zj2.txt' , user= 'student' , group= 'student' )
subprocess
subprocess模块主要用于执行系统命令 subprocess模块允许你产生新的进程,连接到它们的输入/输出/错误管道,并获得它们的返回代码
>> > import subprocess
>> > subprocess. run( [ 'ls' , '/home' ] )
CompletedProcess( args= [ 'ls' , '/home' ] , returncode= 0 )
>> > subprocess. run( [ 'ls' , '/root' ] )
05 1. py test zhuji
CompletedProcess( args= [ 'ls' , '/root' ] , returncode= 0 )
>> > subprocess. run( 'ls' )
test 05 1. py
CompletedProcess( args= 'ls' , returncode= 0 )
>> > subprocess. run( 'ls ~' , shell= True )
05 1. py nsd2019 test zhuji
CompletedProcess( args= 'ls ~' , returncode= 0 )
>> > a = subprocess. run( 'ls ~' , shell = True )
05 1. py nsd2019 test zhuji
>> > a
CompletedProcess( args= 'ls ~' , returncode= 0 )
>> > a = subprocess. run( 'ls ~' , shell= True , stdout= subprocess. PIPE, stderr= subprocess. PIPE)
>> > a
CompletedProcess( args= 'ls ~' , returncode= 0 , stdout= b'05\n1.py\ntest\nzhuji\n' , stderr= b'' )
>> > a. stdout
b'05\n1.py\nnsd2019\ntest\nzhuji\n'
>> > print ( a. stdout. decode( ) )
05
1. py
test
zhuji
time
时间戳:自1970年1月1日0时到某一个时间点之间的秒数 UTC时间:世界协调时。
>> > time. time( )
1584495297.0802724
>> > time. sleep( 3 )
>> > time. ctime( )
'Wed Mar 18 09:36:50 2020'
>> > time. localtime( )
time. struct_time( tm_year= 2020 , tm_mon= 3 , tm_mday= 18 , tm_hour= 9 , tm_min= 37 , tm_sec= 14 , tm_wday= 2 , tm_yday= 78 , tm_isdst= 0 )
>> > t1 = time. localtime( )
>> > t1. tm_mday
18
>> > t1. tm_yday
78
>> > time. strftime( '%Y-%m-%d %a %H:%M:%S' )
'2020-03-18 Wed 09:41:07'
>> > t2 = time. strptime( '2019-3-10 12:30:00' , '%Y-%m-%d %H:%M:%S' )
import time
logfile = 'mylog.log'
t9 = time. strptime( '2019-05-15 09:00:00' , '%Y-%m-%d %H:%M:%S' )
t12 = time. strptime( '2019-05-15 12:00:00' , '%Y-%m-%d %H:%M:%S' )
with open ( logfile) as fobj:
for line in fobj:
t = time. strptime( line[ : 19 ] , '%Y-%m-%d %H:%M:%S' )
if t > t12:
break
if t > t9:
print ( line, end= '' )
datetime
>> > from datetime import datetime
>> > datetime. now( )
datetime. datetime( 2020 , 3 , 18 , 10 , 48 , 16 , 934977 )
>> > t. year, t. month, t. day, t. hour, t. minute, t. second, t. microsecond
( 2020 , 3 , 18 , 10 , 49 , 16 , 189267 )
>> > t. strftime( '%Y-%m-%d %H:%M:%S' )
'2020-03-18 10:49:16'
>> > datetime. strptime( '2050-1-1 0:00:00' , '%Y-%m-%d %H:%M:%S' )
datetime. datetime( 2050 , 1 , 1 , 0 , 0 )
>> > from datetime import datetime, timedelta
>> > days = timedelta( days= 100 , hours= 1 )
>> > t = datetime. now( )
>> > t + days
datetime. datetime( 2020 , 6 , 26 , 12 , 19 , 17 , 90805 )
>> > t - days
datetime. datetime( 2019 , 12 , 9 , 10 , 19 , 17 , 90805 )
from datetime import datetime
logfile = 'mylog.log'
t9 = datetime. strptime( '2019-05-15 09:00:00' , '%Y-%m-%d %H:%M:%S' )
t12 = datetime. strptime( '2019-05-15 12:00:00' , '%Y-%m-%d %H:%M:%S' )
with open ( logfile) as fobj:
for line in fobj:
t = datetime. strptime( line[ : 19 ] , '%Y-%m-%d %H:%M:%S' )
if t > t12:
break
if t > t9:
print ( line, end= '' )
os
>> > os. getcwd( )
'/root/nsd2019/nsd1910/py02/day01'
>> > os. listdir( )
>> > os. listdir( '/tmp' )
>> > os. mkdir( '/tmp/demo' )
>> > os. mkdir( '/tmp/nsd1910/demo' )
>> > os. makedirs( '/tmp/nsd1910/demo' )
>> > os. chdir( '/tmp/demo' )
>> > os. environ[ 'PATH' ]
'/usr/local/bin:/usr/local/sbin:/usr/bin:/usr/sbin:/bin:/sbin:/root/bin:/root/bin'
>> > os. listdir( )
[ ]
>> > import shutil
>> > shutil. copy( '/etc/hosts' , '.' )
'./hosts'
>> > shutil. copytree( '/etc/security' , 'anquan' )
'anquan'
>> > os. symlink( '/etc/passwd' , 'mima' )
>> > os. mknod( 'mytest' )
>> > os. listdir( )
[ 'hosts' , 'anquan' , 'mima' , 'mytest' ]
>> > os. rename( 'mytest' , 'mytest.txt' )
>> > os. listdir( )
[ 'hosts' , 'anquan' , 'mima' , 'mytest.txt' ]
>> > os. chmod( 'hosts' , 0o755 )
>> > os. chmod( 'hosts' , 0o644 )
>> > shutil. rmtree( 'anquan' )
>> > os. remove( 'hosts' )
>> > os. rmdir( '/tmp/nsd1910/demo' )
>> > os. stat( '/etc/hosts' )
os. stat_result( st_mode= 33188 , st_ino= 67160698 , st_dev= 64768 , st_nlink= 1 , st_uid= 0 , st_gid= 0 , st_size= 158 , st_atime= 1584492969 , st_mtime= 1370615492 , st_ctime= 1580547233 )
>> > a = os. stat( '/etc/hosts' )
>> > a. st_size
158
>> > os. listdir( )
[ 'mima' , 'mytest.txt' ]
>> > os. path. abspath( 'mytest.txt' )
'/tmp/demo/mytest.txt'
>> > os. path. basename( '/tmp/demo/mytest.txt' )
'mytest.txt'
>> > os. path. dirname( '/tmp/demo/mytest.txt' )
'/tmp/demo'
>> > os. path. split( '/tmp/demo/mytest.txt' )
( '/tmp/demo' , 'mytest.txt' )
>> > os. path. join( '/tmp/demo' , 'mytest.txt' )
'/tmp/demo/mytest.txt'
>> > os. path. splitext( 'hello.txt' )
( 'hello' , '.txt' )
>> > os. path. isabs( '/sss/aaa/ccc/' )
True
>> > os. path. isdir( '/etc' )
True
>> > os. path. isfile( '/etc' )
False
>> > os. path. islink( '/etc/grub2.cfg' )
True
>> > os. path. ismount( '/' )
True
>> > os. path. exists( '/tmp/xyz.txt' )
False
[ root@localhost demo] cp / etc/ { hosts, passwd, issue} .
[ root@localhost demo] touch aaa/ { a, b, c} . txt
[ root@localhost demo] touch bbb/ { d, e} . doc
[ root@localhost demo] touch ccc/ { f, g} . jpg
[ root@localhost demo] ls - R / tmp/ demo/
/ tmp/ demo/ :
aaa bbb ccc hosts issue passwd
/ tmp/ demo/ aaa:
a. txt b. txt c. txt
/ tmp/ demo/ bbb:
d. doc e. doc
/ tmp/ demo/ ccc:
f. jpg g. jpg
>> > list ( os. walk( '/tmp/demo' ) )
>> > a = list ( os. walk( '/tmp/demo' ) )
>> > len ( a)
4
>> > a[ 0 ]
( '/tmp/demo' , [ 'aaa' , 'bbb' , 'ccc' ] , [ 'hosts' , 'passwd' , 'issue' ] )
>> > a[ 1 ]
( '/tmp/demo/aaa' , [ ] , [ 'a.txt' , 'b.txt' , 'c.txt' ] )
>> > a[ 2 ]
( '/tmp/demo/bbb' , [ ] , [ 'd.doc' , 'e.doc' ] )
>> > a[ 3 ]
( '/tmp/demo/ccc' , [ ] , [ 'f.jpg' , 'g.jpg' ] )
import os
for _path, _dir, _file in list ( os. walk( '/tmp/demo' ) ) :
print ( _path)
for d in _dir:
print ( '\033[34;1m%s\033[0m' % d, end= '\t' )
for f in _file:
print ( f, end= '\t' )
print ( '\n' )
/ tmp/ demo:
aaa bbb ccc hosts passwd issue
/ tmp/ demo/ aaa:
a. txt b. txt c. txt
/ tmp/ demo/ bbb:
d. doc e. doc
/ tmp/ demo/ ccc:
f. jpg g. jpg
pickle
普通的文件操作,只能把str或bytes类型数据写入文件 pickle模块可以把任意的数据类型存到文件中,还可以无损地取出来
>> > import pickle
>> > shopping_list = [ 'fbm' , 'sj' , 'pg' ]
>> > with open ( '/tmp/a.data' , 'wb' ) as fobj:
. . . pickle. dump( shopping_list, fobj)
>> > import pickle
>> > with open ( '/tmp/a.data' , 'rb' ) as fobj:
. . . l1 = pickle. load( fobj)
. . .
>> > type ( l1)
< class 'list' >
>> > l1
[ 'fbm' , 'sj' , 'pg' ]