路径操作
路径操作模块
os模块
os属性
os.name # windows是nt, linux是posix
os.uname() # *nix支持
sys.platform #sys模块的属性, windows显示win32, linux显示linux
os方法
os. listdir( 'o:/tmp' )
os. stat( path, * , dir_fd= None , follow_symlinks= True )
os. lstat( )
os. stat_result( )
os. chmod( path, mode, * , dir_fd= None , follow_symlinks= True )
os.path属性
path. altsep
path. curdir
os.path方法
p = path. join( 'etc' , 'sysconfig' , 'network-scrips' , 'ifcfg-ens33' )
path. exists( p)
path. split( p)
path. dirname( p)
path. basename( p)
path. abspath( p)
path. abspath( '' ) == path. abspath( '.' )
path. splitdrive( p)
path. splitext( p)
path. commonpath( 容器)
path. commonprefix( 列表或元组)
p1 = path. abspath( __file__)
while p1 != path. dirname( p1) :
p1 = path. dirname( p1)
print ( p1)
pathlib模块Path类
Path类的方法和属性
Path. cwd( )
Path. home( )
Path对象
p = Path( '/etc' , 'sysconfig' , Path( ) )
p = Path( ) == p is Path( '' ) is p = Path( '.' )
str ( p)
bytes ( p)
Path对象操作符
Path对象 / Path对象, 生成一个新的Path对象
Path对象 / 字符串, 生成一个新的Path对象
字符串 / Path对象, 生成一个新的Path对象
Path对象属性
p. parent
p. parents
p. name
p. stem
p. suffix
p. suffixes
name = stem + suffix
Path对象方法
p. with_suffix( '.az' )
p. with_name( 'hello' )
p. cwd( )
p. home( )
p. resolve( )
p. absolute( )
p. mkdir( mode= 0o777 , parents= False , exist_ok= False )
p. rmdir( )
p. touch( mode= 0o666 , exist_ok= True )
p. as_uri( )
p. iterdir( )
p. glob( "*.*" )
p. rglob( "*.?" )
Path( 'a/b/c.py' ) . match( '*.py' )
p. stat( )
p. lstat( )
p. open ( mode= 'r' , buffering= - 1 , encoding= None , error= None , newline= None )
p. read_bytes( )
p. read_text( encoding= None , errors= None )
p. write_bytes( data)
p. write_text( data, encoding= None , errors= None )
for x in p. parents[ len ( p. parents) - 1 ] . iterdir( ) :
print ( x, end= ' ' )
if x. is_dir( ) :
flag = False
for _ in x. iterdir( ) :
flag = True
break
print ( 'dir{},{}' . format ( x, 'Not empty' if flag else 'empty' ) )
elif x. is_file( ) :
print ( "{}is file" . format ( x) )
else :
print ( 'other' )
p. exists( )
p. is_dir( )
p. is_file( )
p. is_symlink( )
p. is_socket( )
p. is_block_device( )
p. is_char_device( )
P. is_absolute( )