Python 更好的文件路径处理模块 pathlib

发布时间 2023-03-23 15:17:02作者: pu369com

pathlib 创建的Path对象,可以非常方便地操作目录及文件

import pathlib

path = pathlib.Path.cwd()
print("当前路径:",path)

for item in path.iterdir():
    if item.is_file():
        print("file:"+str(item))
    elif item.is_dir():
        print("dir:",item)
    else:
        print("啥也不是")


path =  path / 'a.txt'
print("生成一个文件路径:",path)
path.write_text('中国')
path.write_bytes(b"abc")#没有追加模式,覆盖了上一行的内容
btxt = path.read_bytes()
print(btxt)

  

 

 

参考:https://blog.csdn.net/be5yond/article/details/119752040

http://www.manongjc.com/detail/39-tprileesswqtabb.html

https://www.cnblogs.com/xy-bot/p/16370044.html

http://www.manongjc.com/detail/39-tprileesswqtabb.html