Python的os.path和sys模块

python中使用os模块中的path模块进行文件路径的读取和操作。通过sys.path来对导入模块的检索路径进行操作。

 

import os导入模块。

PS: 在开始之前先说明下, __file__文件为该执行的文件的相对路径的文件名而已。所以在当前目录下运行即文件名本身,在上一层路径下运行,则是file所在的路径+文件名的相对路径。

路径相关-----------------------------------------------------------------------------------------------------------------------------------------------------

os.getcwd() 

获取当前目录路径(路径,不包含文件名)

此方法获取的并不是该执行文件所在的目录,是python命令运行的路径。

如 该文件为 HOME/py/path.py,如果是在HOME/py/目录下用python path.py运行,打印/home/xxx/py

但是如果cd到上一层目录, 通过  python ./py/path.py运行,  打印/home/xxx/

 

os.path.abspath(__file__)

获取文件的绝对路径(含文件名),当然也可以使用os.path.abspath("home/whj/file")这样的绝对路径打印该路径,所以该函数主要就是将括弧中的绝对路径打印(完全的路径,包含文件名),该方法与在何层目录下运用命令无关。

 

os.path.dirname(path)

返回文件路径。

如os.path.dirname(__file__)在当前目录下python命令运行该文件,打印的为空(因为__file__只是相对路径,即文件名而已。)假设该文件位于名为/ss/的路径下的path.py文件,现在cd到上一层路径,用python  ./ss/path.py运行,则打印的是./ss。当然如果使用python  ss/path.py运行,打印的是ss。

当然也可以在括弧中书写路径,如os.path.dirname("/home/czc"),打印的是/home   但os.path.dirname("/home/czc/")  打印的是/home/czc

可件对于路径而言,有无“/”意义不同。

 

os.path.basename(path)

返回文件名,只返回文件名,如果只是路径,则返回为空。

 

os.path.split(path)

 

把路径分割成 dirname 和 basename,返回一个元组。

 

os.path.join(path1[, path2[, ...]])

把目录和文件名合成一个路径

 

os.path.exists(path)

判断该目录(严格而言应该叫做路径,因为也会判断文件是否存在)是否存在。

返回值为   True  或者  False 。

 

os.path.isfile(path)

判断该文件(必须是文件,路径则返回False)是否存在。

返回值为   True  或者  False 。

 

os.path.isdir(path)

判断该路径(必须是路径,文件则返回False)是否存在。

返回值为   True  或者  False 。

 

os.path.islink(path)

判断路径是否为链接

路径相关-----------------------------------------------------------------------------------------------------------------------------------------------------

 

 

路径导入相关------------------------------------------------------------------------------------------------------------------------------------------------

python中sys.path是导入模块的路径,是一个list。(即PYTHONPATH)你可以用list的append、insert、remove、pop、sort之类的方法修改。

可以在python 环境下使用sys.path.append(path)添加相关的路径,但在退出python环境后自己添加的路径就会自动消失!

import sys
sys.path.append(Path)

 

可以选择用sys.path.insert(0,‘/path’),这样新添加的目录会优先于其他目录被import检查 

sys.path.insert(0, '引用模块的地址') 

 

sys.path.remove("xxxxx") 去掉地址

 

 

#! /usr/bin/env python
# -*- coding:UTF-8 -*-

from __future__ import print_function
import os

# ---------------------------------------------------------------------------------------------------------------------------------------------------------
current_path =  os.getcwd()
abspath_path = os.path.abspath(__file__)
abspath_path_write_path = os.path.abspath("/home/czc/")
abspath_path_write_file = os.path.abspath("/home/czc/1.txt")
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
dir_path = os.path.dirname(__file__)                                    # __file__是相对路径,如在此根目录下运行是空白,如在上一层目录下,则是上一层目录的名
dir_abspath_path = os.path.dirname(os.path.abspath(__file__))           # 绝对路径下,则无论在何层运行都为同一结果,即上一层目录名的全路径
dir_abspath_path_write = os.path.dirname("/home/czc/")                  # 最终打印的是/home/czc
dir_abspath_path_write_ = os.path.dirname("/home/czc")                  # 最终打印的是/home
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
basename = os.path.basename(__file__)
basename_write_path = os.path.basename("/home/czc/")                    # 因为是路径名,所以打印出来的为空
basename_write_file = os.path.basename("/home/czc")                     # 认为czc为文件,所以打印czc
basename_ = os.path.basename(os.path.abspath(__file__))
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
path_exists = os.path.exists(os.path.abspath(__file__))                 # 为真
path_exists_write = os.path.exists("/home/czc/qq.txt")                  # 为假  所以所谓路径即含文件名 所以当文件不存在的,亦为假
path_exists_write__path = os.path.exists("/home/czc/")
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
file_isfile = os.path.isfile(os.path.abspath(__file__))
file_isfile_ = os.path.isfile(__file__)
file_isfile_path = os.path.isfile("/home/czc/")                        # 因为是路径,所以为假,不管带不带“/”,都为假
file_isfile_path_ = os.path.isfile("/home/czc")     
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
dir_isdir_file = os.path.isdir(os.path.abspath(__file__))
dir_isdir_path = os.path.isdir("/home/czc/")
# ---------------------------------------------------------------------------------------------------------------------------------------------------------
print("当前的文件路径是:{}".format(current_path))
print("py文件的绝对路径是:{}".format(abspath_path))
print("手写的绝对路径(不含文件名带“/”)是:{}".format(abspath_path_write_path))
print("手写的绝对路径(含文件名)是:{}".format(abspath_path_write_file))
print("py文件的父目录是:{}".format(dir_path ))
print("py文件绝对路径的父目录是:{}".format(dir_abspath_path))
print("手写的路径(不含文件名带“/”)的父目录是:{}".format(dir_abspath_path_write))
print("手写的路径(含文件名,即无“/”)的父目录是:{}".format(dir_abspath_path_write_))
print("py文件的相对路径文件名是:{}".format(basename))
print("py文件的绝对路径文件名是:{}".format(basename_))
print("手写的路径(不含文件)的文件名是:{}".format(basename_write_path))
print("手写的路径(含文件, 即最后没有“/”)的文件名是:{}".format(basename_write_file))
print("判断py文件绝对路径(含文件名,文件名为真)是否存在:{}".format(path_exists))
print("判断手写文件绝对路径(含文件名,路径为真,文件名为假)是否存在:{}".format(path_exists_write))
print("判断手写文件绝对路径(不含文件名,路径为真)是否存在:{}".format(path_exists_write__path))
print("判断文件(绝对路径)是否是文件:{}".format(file_isfile))
print("判断文件(相对路径)是否是文件:{}".format(file_isfile_))
print("判断手写路径(带“/”)是否是文件:{}".format(file_isfile_path))
print("判断手写路径(不带“/”)是否是文件:{}".format(file_isfile_path_))
print("判断手写文件路径是路径:{}".format(dir_isdir_file))
print("判断手写路径(带“/”)是路径:{}".format(dir_isdir_path))

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值