Python之文件操作

创建目录

import os
import errno
def 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 文件路径操作
接口 简介
os.path.abspath(path) 获取path绝对路径
os.path.split(path) 将path分割为目录和文件名的二元的元组
os.path.dirname(path) path的目录
os.path.basename(path) 返回最后文件的文件名,如果以/或\结尾,返回为空
os.path.commonprefix(pathlist) 返回pathlist中共有的最长的路径
os.path.join(pathlist) 将多个路径组合后返回,第一个路径是绝对路径之前的路径参数将忽略
os.path.normcase(path) Linux和Mac平台,原样返回,windows平台会将所有的路径中的字符转化为小写,将所有的斜杠转化为反斜杠
os.path.normpath(path) 规范化文件的路径
os.path.splitdrive 返回由driver名和路径组成的二元的元组
import os

absPath=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 None
baseName=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 path
normName=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.py
Location : . File : file.py
Abs location : D:\zmakeup\00-code\00-python\00-test File : file.py
Directory name : .
abs Directory name D:\zmakeup\00-code\00-python\00-test
Base name file.py
Longest Path :D:\zmakeup
Joined path : D:/zmakeup\file.py
Norm case Name d:\programs\python\python36-32\python.exe
Norm name D:\gstreamer\1.0\x86_64\etc
Driver D: others /zmakeupile.py
  • 2 文件属性判断
接口 简介
os.path.exists(path) 判断一个文件是否存在,True:存在 False:不存在
os.path.isabs(path) 判断该路径是某是绝对路径
os.path.isfile(path) 判断path是否时存在的文件
os.path.isdir(path) 判断path是否是一个存在的目录
import os

if 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 existed
This path is not absolute path
This is a file.
This is not a directory.
  • 3 文件属性获取
接口 简介
os.path.splitext(path) 返回文件名和扩展名组成的二元的元组
os.path.getsize(path) 获取文件的大小
os.path.getatime(path) 获取文件或者目录最后存取的时间
os.path.getmtime(path) 获取文件或者目录最后修改的时间
import os

extension=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 

你可能感兴趣的:(python)