Python学习week7-文件路径操作

Posted soulgou

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python学习week7-文件路径操作相关的知识,希望对你有一定的参考价值。

1、os.path模块

# os.path常用方法

from os import path
p=path.join(/etc,sysconfig,network)  # 路径拼接
print(type(p),p)
print(path.exists(p)) # 判断路径是否存在

print(path.split(p)) #  将路径切割为2部分,head,tail

print(path.abspath(.)) # 当前路径的绝对路径

p=path.join(o:/,p,test.txt)
print(p)  # o:/etcsysconfig
etwork	est.txt
print(path.dirname(p)) # o:/etcsysconfig
etwork
print(path.basename(p)) # test.txt

p1=path.abspath(__file__)
while p1 != path.dirname(p1):
    p1=path.dirname(p1)
    print(p1,path.basename(p1))
    
‘‘‘
运行结果:
F:study-pythoncom.lx.studyweek7 week7
F:study-pythoncom.lx.study com.lx.study
F:study-python study-python
F: 
‘‘‘

2、pathlib模块

# python3.4版本引入pathlib模块,提供Path对象来操作,包括目录和文件;

 

以上是关于Python学习week7-文件路径操作的主要内容,如果未能解决你的问题,请参考以下文章

python学习笔记_week7

学习笔记:python3,代码片段(2017)

机器学习week7 ex6 review

Coursera机器学习week7 单元测试

吴恩达-coursera-机器学习-week7

Coursera机器学习week7 笔记