In:frompathlibimportPathIn:Path.home()Out:PosixPath('/Users/dongweiming')# 用户目录In:path=Path('/user')In:path/'local'# 非常直观Out:PosixPath('/user/local')In:str(path/'local'/'bin')Out:'/user/local/bin'In:f=Path('example.txt')In:f.write_bytes('This is the content'.encode('utf-8'))Out[16]:19In:withf.open('r',encoding='utf-8')ashandle:# open现在是方法了....:print('read from open(): {!r}'.format(handle.read()))....:readfromopen():'This is the content'In:p=Path('touched')In:p.exists()# 集成了多个常用方法Out:FalseIn:p.touch()In:p.exists()Out:TrueIn:p.with_suffix('.jpg')Out:PosixPath('touched.jpg')In:p.is_dir()Out:FalseIn:p.joinpath('a','b')Out:PosixPath('touched/a/b')
In:name='Fred'In:f'My name is {name}'Out:'My name is Fred'In:fromdatetimeimport*In:date=datetime.now().date()In:f'{date} was on a {date:%A}'Out:'2018-01-17 was on a Wednesday'In:deffoo():....:return20....:In:f'result={foo()}'Out:'result=20'
可以的 刚才偶尔 bug 可能