1 os.path.exists()
转自:https://blog.csdn.net/u012424313/article/details/82216092
os即operating system(操作系统),Python 的 os 模块封装了常见的文件和目录操作。
os.path模块主要用于文件的属性获取,exists是“存在”的意思,所以顾名思义,os.path.exists()就是判断括号里的文件是否存在的意思,括号内的可以是文件路径。
举个栗子:
user.py为存在于当前目录的一个文件
输入代码:
import os
path = os.path.exists('user.py')
print(path)
输出:
True
Process finished with exit code 0
2 os.mkdir()与os.makedirs()
https://blog.csdn.net/Beyond_F4/article/details/80430911
首先说os.mkdir(path),他的功能是一级一级的创建目录,前提是前面的目录已存在,如果不存在会报异常,比较麻烦,但是存在即有他的道理,当你的目录是根据文件名动态创建的时候,你会发现他虽然繁琐但是很有保障,不会因为你的一时手抖,创建而创建了双层或者多层错误路径,
import os
os.mkdir('d:\hello') # 正常
os.mkdir('d:\hello\hi') # 正常
# 如果d:\hello目录不存在
# 则os.mkdir('d:\hello\hi')执行失败
然后是os.makedirs(path),单从写法上就能猜出他的区别,他可以一次创建多级目录,哪怕中间目录不存在也能正常的(替你)创建,想想都可怕,万一你中间目录写错一个单词.........
import os
os.makedirs('d:\hello') # 正常
os.makedirs('d:\hello\hi') # 正常
# 如果d:\hello目录不存在
# 则os.mkdir('d:\hello\hi') # 仍然正常
3 os.path.join()
https://blog.csdn.net/weixin_37895339/article/details/79185119
os.path.join()函数用于路径拼接文件路径。
os.path.join()函数中可以传入多个路径:
会从第一个以”/”开头的参数开始拼接,之前的参数全部丢弃。
有多个以”/”开头的参数,从最后”/”开头的的开始往后拼接,之前的参数全部丢弃。
以上一种情况为先。在上一种情况确保情况下,若出现”./”开头的参数,会从”./”开头的参数的上一个参数开始拼接。
import os
print("1:",os.path.join('aaaa','/bbbb','ccccc.txt'))
print("2:",os.path.join('/aaaa','/bbbb','/ccccc.txt'))
print("3:",os.path.join('aaaa','./bbb','ccccc.txt'))
输出为:
1: /bbbb\ccccc.txt
2: /ccccc.txt
3: aaaa\./bbb\ccccc.txt
在windows下的字符串中 正反斜杠都可以用来作为 目录分隔符
python默认采用的是linux的语法
4 os.path.dirname(__file__)
原文:https://blog.csdn.net/u011760056/article/details/46969883
os.path.dirname(__file__)返回脚本的路径,但是需要注意一下几点:
1、必须是实际存在的.py文件,如果在命令行执行,则会引发异常NameError: name '__file__' is not defined
2、在运行的时候如果输入完整的执行的路径,
则返回.py文件的全路径如:
python c:/test/test.py
则返回路径 c:/test ,
如果是python test.py 则返回空
3、结合os.path.abspath用,效果会好,如果大家看过一些python架构的代码的话,会发现经常有这样的组合
os.path.dirname(os.path.abspath(__file__)),os.path.abspath(__file__)返回的是.py文件的绝对路径
5 os.path.abspath(__file__) 获取脚本完整路径
最后获得该文件的完整路径
一大波OS的返回显示:
https://blog.csdn.net/ningning52107/article/details/80245901
#coding=utf-8
import os
print os.path.abspath("d:\\new\\test.txt") #d:\new\test.txt
print os.path.basename("d:\\new\\test.txt") #test.txt
print os.path.dirname("d:\\new\\test.txt") #d:\new
print os.path.exists("d:\\new") #True
print os.path.lexists("d:\\new") #True
print os.path.expanduser("d:\\new\\text.txt") #d:\new\text.txt
print os.path.getatime("d:\\new") #最后访问时间
print os.path.getmtime("d:\\new") #最后修改路径时间
print os.path.getctime("d:\\new") #创建时间
print os.path.getsize("d:\\new\\") #或许路径的大小 字节为单位
print os.path.isabs("d:\\") #是否是绝对路径
print os.path.isfile("d:\\new\\hello.txt")
print os.path.isdir("d:\\new")
print os.path.islink("d:\\new\\hello.txt")
print os.path.join("d:\\new","hello.txt")
print os.path.normcase("d:\\new\\hello.txt")
print os.path.relpath("d:\\new\\hello.txt") #相对路径
print os.path.split("d:\\new\\hello.txt") #分离文件名
print os.path.splitdrive("d:\\new\\hello.txt") #分离磁盘驱动器
print os.path.splitext("d:\\new\\hello.txt") #分离扩展名
os.path.splitext()
split() 用于返回目录路径和文件名的元组
import os
os.path.split('d:\\library\\book.txt') #输出('d:\\library', 'book.txt')
splitdrive() 用于返回盘符和路径字符元组
os.path.splitdrive('d:\\library\\book.txt') #输出('d:', '\\library\\book.txt')
splitext() 用于返回文件名和扩展名元组
os.path.splitext('d:\\library\\book.txt')('d:\\library\\book', '.txt')
os.path.splitext('book.txt')('book', '.txt')
所以os.path.splitext(os.path.basename(in_path.decode(“utf8”)))
path10=os.path.splitext(os.path.basename(D:\anaconda\projects\treelstm\treelstm.pytorch-master\scripts\ostest.py))
print(path10)#得到最后一个文件或文件名和它的后缀
结果
('ostest', '.py')