获取文件路径&os模块常用功能

import os,sys
print("sys.path[0] = ", sys.path[0])
print("sys.argv[0] = ", sys.argv[0])
print("__file__ = ", __file__)
print("os.path.abspath(__file__) = ", os.path.abspath(__file__))
print("os.path.realpath(__file__) = ", os.path.realpath(__file__))
print("os.path.dirname(os.path.realpath(__file__)) = ",
       os.path.dirname(os.path.realpath(__file__)))
print("os.path.split(os.path.realpath(__file__)) = ",
       os.path.split(os.path.realpath(__file__)))
print("os.path.split(os.path.realpath(__file__))[0] = ",
       os.path.split(os.path.realpath(__file__))[0])
print("os.getcwd() = ", os.getcwd())
#输出是一直文件上层文件夹的真实路径
os.path.dirname(os.path.realpath(__file__)))
sys.path[0]
os.path.split(os.path.realpath(__file__))[0]
os.getcwd() =  D:\testdm
#输出是真实路径
sys.argv[0] 
__file__
os.path.abspath(__file__)
os.path.realpath(__file__)
#输出是文件名称
os.path.split(os.path.realpath(__file__)) [1]
#os.path是纯粹的字符串操作
os.path.join() #添加路径
###注意
os.path.join("just", "do", "d:/", "python", "dot", "g:/", "com")
# out:  'g:/com'
os.path.exists() #判断是否存在
os.listdir(filePath)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值