1. 程式人生 > >Python學習week7-文件路徑操作

Python學習week7-文件路徑操作

import \n pan 目錄 判斷 com net 結果 style

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:/etc\sysconfig\network\test.txt
print(path.dirname(p)) # o:/etc\sysconfig\network 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-python\com.lx.study\week7 week7 F:\study-python\com.lx.study com.lx.study F:\study-python study-python F:\
‘‘‘

2、pathlib模塊

# python3.4版本引入pathlib模塊,提供Path對象來操作,包括目錄和文件;

Python學習week7-文件路徑操作