当前位置:网站首页>day3·文件与目录
day3·文件与目录
2022-07-22 00:50:00 【慵懒的猫丫头】
__file__
__file__内置特殊变量,返回当前的文件路径包含文件名,常与os.path.dirname(file)方法使用~返回文件的目录
import os.path
# 返回当前文件绝对路径包含文件名
print(__file__)
# 返回当前文件的上一层级的绝对路径
print(os.path.dirname(__file__))
os.path.split(文件或目录)
默认按照/分隔,以最后一个/为分隔符,将/前面的内容和后面的内容以元组形式返回
# 分离文件绝对路径,将目录与文件名组成一个元组返回,第一个是目录,第二个是文件名
print(os.path.split(__file__))
# 单独取文件名~思路分离当前文件目录与文件名xx.xx再利用字符串split方法用.分隔获取第一个值就是文件名
print(os.path.split(__file__)[1].split(".")[0])
print(os.path.split(r"d:"))
os.path.isfile(obj)
判断当前对象是否是一个文件,返回的是布尔型
# 判断当前文件是否是一个文件
print(os.path.isfile(__file__))
os.path.isdir(obj)
判断当前对象是否是一个目录,返回的是布尔型
# 判断当前文件上一个曾经是否是一个目录
print(os.path.isdir(os.path.dirname(__file__)))
os.path.join(目录,文件名)
将指定目录和文件名组装在一起
print(os.path.join(r"d:/","1.jpg")) #返回d:/1.jpg
边栏推荐
猜你喜欢
It can be said to be the best right-click menu management tool so far
rexroth比例阀4WRPEH6C3B04L-3X/M/24F1-885
dom——事件链(捕获目标和捕获)
7.zabbix自动发现.自动注册
BERTopic
Rexroth proportional valve 4wrpeh6c3b04l-3x/m/24f1-885
Interlaced color change of table
State owned enterprises work overtime in 996, but job hopping offers are outsourcing. What should we do?
Seven schemes of Web real-time message push
全链路压测:影子库与影子表之争
随机推荐
微服务架构 | 服务注册发现中心/配置中心/消息总线 - [nacos] TBC...
【每日一题】814. 二叉树剪枝
Web Automation - selenium
解决v-for中el-popover显示等问题
测试的分类
从20s优化到500ms,我用了这三招
kube-scheduler的调度上下文
Raspberry pie - Cloud Server Deployment - intranet penetration - use of cpolar tool
Anomaly detection and self encoder (2)
When wechat is opened, it supports message notification banners to attract hot discussion; The cloud services of Google and Oracle were offline due to the high temperature weather in the UK; Google re
ES6中 async 函数、await表达式
7. ZABBIX auto discovery automatic logon
51 MCU peripherals: LED dot matrix
08.Octave 语言的使用-控制语句、绘制图线和其他命令
Go language go command line tool
3.zabbix安装
Ardunio开发——水泵操作过程
classes.jar: 另一个程序正在使用此文件,进程无法访问。
ABAP grammar foundation 4
Cache and redirection in HTTP practice