from pathlib import Path a = Path . is_dir ( Path . home (). joinpath ( 'data' , 'abc' )) print ( a ) b = Path . is_file ( Path . cwd (). joinpath ( 'pathlib_01.py' )) print ( b ) c = Path . home (). joinpath ( 'data' ). parts print ( c ) #
a = Path.is_dir(Path.home().joinpath('data', 'abc'))
print(a)
b = Path.is_file(Path.cwd().joinpath('pathlib_01.py'))
print(b)
c = Path.home().joinpath('data').parts
print(c)
# 创建单个文件夹, 当要创建的文件夹存在时会报错
p1 = Path.cwd().joinpath('tmp')
if not Path.exists(p1):
Path.mkdir()
# 创建嵌套文件夹,注意加parents参数,否则报错
p2 = Path.cwd().joinpath('tmp1/tmp2')
if not p2.exists():
Path.mkdir(p2, parents=True)