pathlib.Path()

from pathlib import Path
p=Path("/test2")

Path.cwd()

print(p.cwd())
/py3

Path.home()

print(p.home())
/root

Path.iterdir()

print(list(p.iterdir()))
[PosixPath('/test2/p.py'), PosixPath('/test2/aa.txt'), PosixPath('/test2/bb')]

Path.stat()

print(p.stat())
s.stat_result(st_mode=16804, st_ino=296353, st_dev=2053, st_nlink=3, st_uid=500, st_gid=500, st_size=4096, st_atime=1503619754, st_mtime=1503523284, st_ctime=1503523284)

Path.chmod()

print(oct(p.stat().st_mode)[-3:])

p.chmod(0o755)

print(oct(p.stat().st_mode)[-3:])

Path.owner()

print(p.owner())
user00

Path.group()

print(p.group())
user00

Path.glob(pattern)

print(list(p.glob("*.py")))
[PosixPath('/test2/p.py')] print(list(p.glob('**/*.py')))
[PosixPath('/test2/p.py'), PosixPath('/test2/bb/p2.py')]

Path.rglob(pattern)

print(list(p.rglob('*.py')))
[PosixPath('/test2/p.py'), PosixPath('/test2/bb/p2.py')]

Path.expanduser()

p=Path("~/Desktop")
print(p.expanduser())
/root/Desktop

Path.exists()

print(p.exists())
True p=Path("/test2_not_exist")
print(p.exists())
False

Path.is_dir() |  is_file()

print(p.is_dir())
True
print(p.is_file())
False

Path.is_socket() | is_fifo() | is_block_device() | is_char_device() | is_symlink()

Path.mkdir(mode=0o777, parents=False, exist_ok=False)

p=Path("/test2/test2_tree")
p.mkdir(mode=0o644)
print(p.exists())
True p=Path("/test2_no_exist/aa")
p.mkdir()
FileNotFoundError: [Errno 2] No such file or directory: '/test2_no_exist/aa'
p.mkdir(parents=True)
print(p.exists())
True p.mkdir(exist_ok=True)
不会报错

Path.touch(mode=0o666, exist_ok=True)

p=Path("/test2/no_exits_text")
p.touch(mode=0o755)
print(p.exists())
True
print(oct(p.stat().st_mode)[-3:])

Path.open(mode=’r’, buffering=-1, encoding=None, errors=None, newline=None)

p=Path("/test2/aa.txt")
with p.open() as f:
print(f.read())

Path.read_bytes()

print(p.read_bytes())
b'test content\n2test2 content\n'

Path.read_text()

print(p.read_text())
test content
2test2 content

Path.write_bytes(data)

Path.write_text(data,encoding=None,errors=None)

Path.rename(target)p=Path("/test2/test.txt")

p.open("w").write("test content")
target=Path("/test2/test.rename")
p.rename(target)

print(target.open().read())
test content
print(p.exists())
False


Path.replace(target)

无条件替换

Path.resolve()

返回绝对路径

Path.samefile(other_path)

Path.rmdir()

p=Path("/test2/bb")
p.rmdir()
OSError: [Errno 39] Directory not empty: '/test2/bb'
#只能删除空目录

Path.symlink_to()

Path.unlink()

PurePath.suffix

p=Path("/test2/bb/p2.py")
print(p.suffix)
.py
print(p.suffixes)
['.py']

连接路径

print(pathlib.Path('/',"/home","user00"))
/home/user00 def get_home(username):
if username=="root":
return pathlib.Path("/","root")
else:
return pathlib.Path('/','/home',username) print(get_home("root"))
/root
print(get_home("user00"))
/home/user00

[PY3]——IO——pathlib的更多相关文章

  1. [PY3]——IO——文件目录操作

    IO—os.shutil—文件目录操作 目录操作 1. 新建 os.mkdir(path[, mode]) 相当于mkdir,新建目录 os.makedirs(path[, mode]) 相当于mkd ...

  2. [PY3]——IO——文件读写

    文件打开和关闭 # 使用open 打开文件,返回时值是一个 File-like对象 f.open('/test/file') # 使用read读取文件 f.read( ) # 使用close关闭文件 ...

  3. 重启服务器后Docker容器暴露的端口外网突然访问不了!!

    *:first-child { margin-top: 0 !important; } .markdown-body>*:last-child { margin-bottom: 0 !impor ...

  4. VS2015编译GEOS

    下载链接:http://trac.osgeo.org/geos/ 1. 打开cmake,加载geos源码和定位geos的工程存放位置: 2.点击configure,会报错,首先设置CMAKE_INST ...

  5. 【python】pathlib库

    pathlib在python3.2以上开始默认支持,在python2.7中如果要使用需要安装 pip install pathlib pathlib更多参考资料:http://pathlib.read ...

  6. python运维开发(十)----IO多路复用线程基本使用

    内容目录: python作用域 python2.7和python3.5的多继承区别 IO多路复用 socketserver模块源分析 多线程.进程.协程 python作用域  python中无块级作用 ...

  7. py2与py3区别总结

    1. py2中的str是py3中的bytes py2中的Unicode是py3中的str 声明一个字符串变量时,py2 和py3都是str类型,但py2代表字节类型,py3代表文本类型 隐式转换: p ...

  8. (转)pathlib路径库使用详解

    原文:https://xin053.github.io/2016/07/03/pathlib%E8%B7%AF%E5%BE%84%E5%BA%93%E4%BD%BF%E7%94%A8%E8%AF%A6 ...

  9. [py]django上线部署-uwsgi+nginx+py3/django1.10

    https://github.com/lannyMa/django-uwsgi-nginx.git 单机调试启动-确保项目代码没问题 - 克隆代码进入项目 git clone https://gith ...

随机推荐

  1. Programmatically Disable Event Firing on List Item Update in SharePoint 2010

    1. Microsoft.SharePoint.dll Create EventFiring.cs 1.Right-click on the project, select Add and click ...

  2. iOS 优秀开源项目

    1. 界面 iCarousel: 用来做图片旋转显示效果(相册). MZTimerLabel:用来制作一个计时并显示时间的 label. MSDynamicsDrawerViewController, ...

  3. docker-runc not installed on system 问题

    问题描述: docker运行镜像的时候,报错如下: 1 2 3 [root@etcd1 k8s]# docker run -it registry.helloworld.com/test/atsd:l ...

  4. mysql--多表连接查询

    一.多表连接查询 SELECT 字段列表 FROM 表1 INNER|LEFT|RIGHT JOIN 表2 ON 表1.字段 = 表2.字段; 1.交叉连接:不适用任何匹配条件.生成笛卡尔积 sele ...

  5. Android Studio如何用真机调试

    1,在真机中设置USB调试模式打开,具体:“设置”->“应用程序”->“开发”->“USB调试”. 2,安装安卓的USB驱动,如果按照不好,那么去下载一个豌豆荚,它会帮你正确安装你的 ...

  6. Python3.5 学习一

    初期学习,离不了环境搭建及语言的基本语法等. Python属于动态解析.跨平台. 前期了解了Pyhon环境搭建,在Linux(ubuntu)和windows上都有所学习了解,由于不再当前所学资料教程内 ...

  7. 【Oracle 12c】最新CUUG OCP-071考试题库(56题)

    56.(14-14) choose the best answer: You need to create a table with the following column specificatio ...

  8. ElasticSearch NEST搜索

    var client = ElasticsearchHelper.GetElasticClient("order");QueryContainer termQuery = new ...

  9. [实战] 给现有的NGINX二进制RPM包加新模块

    [实战] 给现有的NGINX二进制RPM包加新模块 一.前言 在做 wiki 的镜像,这样以后文章就可以使用外链了(链接直接跳转墙内小站). 遇到的问题就是:我的 NGINX 包安装的时候图方便采用 ...

  10. jmeter结果分析(图形报表和聚合报告)

    采用Jmeter测试工具对web系统作的负载测试,得出的响应报表,数据比较难懂,现作一具体说明.以下是在一次具体负载测试中得出的具体数值,测试线程设置情况为:线程数:200,等待时间(ramp-up) ...