Python之文件操作
来源:互联网 发布:迪优美特c6网络盒子 编辑:程序博客网 时间:2024/05/24 01:30
创建目录
import osimport errnodef mkdir(dir): try: os.makedirs(dir) except OSError as exc: if exc.errno == errno.EEXIST: print("The dir has been existed !") pass else: raise#创建名为test的目录以及其名为00的子目录mkdir("test/00")
获取文件属性
- 1 文件路径操作
import osabsPath=os.path.abspath('file.py')print("Abspath :" + absPath)splitPath=os.path.split('./file.py')print("Location : %s File : %s" % (splitPath[0], splitPath[1]))splitPath=os.path.split(os.path.abspath('file.py'))print("Abs location : %s File : %s" % (splitPath[0], splitPath[1]))dirName=os.path.dirname("./file.py")print("Directory name : " + dirName)absDirName=os.path.dirname(os.path.abspath("file.py"))print("abs Directory name " + absDirName)#end with / or \ basename return NonebaseName=os.path.basename("file.py")print("Base name " + baseName)pathList=["D:\\zmakeup", "D:\\zmakeup\\00-code\\00-python\\00-test", "D:\\zmakeup\\00-code\\"]longestPath=os.path.commonprefix(pathList)print("Longest Path :" + longestPath)joinPath=os.path.join("../","D:/zmakeup",'file.py')print("Joined path : " + joinPath)normcaseName=os.path.normcase("D:\Programs\Python\Python36-32\python.exe")print("Norm case Name " + normcaseName)# note the format of the pathnormName=os.path.normpath("D://gstreamer\\1.0//x86_64\\bin//../etc")print("Norm name " + normName)splitDriveName=os.path.splitdrive("D:/zmakeup\file.py")print("Driver %s others %s" % (splitDriveName[0], splitDriveName[1]))
返回的结果
Abspath :D:\zmakeup\00-code\00-python\00-test\file.pyLocation : . File : file.pyAbs location : D:\zmakeup\00-code\00-python\00-test File : file.pyDirectory name : .abs Directory name D:\zmakeup\00-code\00-python\00-testBase name file.pyLongest Path :D:\zmakeupJoined path : D:/zmakeup\file.pyNorm case Name d:\programs\python\python36-32\python.exeNorm name D:\gstreamer\1.0\x86_64\etcDriver D: others /zmakeupile.py
- 2 文件属性判断
import osif os.path.exists("file.py"): print("file.py existed")else: print("file.py not existed")if os.path.isabs("file.py"): print("This path is absolute path")else: print("This path is not absolute path")if os.path.isfile("file.py"): print("This is a file.")else: print("This is not a file")if os.path.isdir("file.py"): print("This is a directory.")else: print("This is not a directory.")
实验结果
file.py existedThis path is not absolute pathThis is a file.This is not a directory.
- 3 文件属性获取
import osextension=os.path.splitext("D://zmakeup\\00-code\\00-python\\00-test\\file.py")print("Location %s entension %s " % (extension[0], extension[1]))size=os.path.getsize('file.py')stime=os.path.getatime('file.py')mtime=os.path.getmtime('file.py')print("size %u stime %u mtime %u " % (size, stime, mtime))
实验结果
Location D://zmakeup\00-code\00-python\00-test\file entension .py size 2396 stime 1510046250 mtime 1510046250
阅读全文
0 0
- python之文件操作
- python之文件操作
- python 之文件操作
- python 之文件操作
- python之文件操作
- Python之文件操作
- python之文件操作
- python之文件操作
- python之文件操作
- python之文件操作
- Python之文件操作
- Python之文件操作
- Python 之文件操作
- PYTHON笔记之文件操作
- Python学习之文件操作
- Python学习之文件操作
- Python之操作Excel文件
- python基础之文件操作
- Docker-1.什么是Docker
- jacob 另存word ,excel 为html,xml格式
- 选择106短信平台,你所不知道的内幕常识
- 从Exchager数据交换到基于trade-off的系统设计
- java中的接口
- Python之文件操作
- java SE教程04
- 开始
- 服务计算作业3——agenda(测试)
- iOS网络请求错误nw_proxy_resolver_create_parsed_array PAC evaluation error: NSURLErrorDomain: -1004
- 效果图第四课:修改器建模
- C++ 简单实现HTTP GET/POST 请求
- cocoaPods 安装方法
- Docker学习(3)构建Jenkins和Docker服务器