Python文件操作

Python常见文件操作示例

  • os.path 模块中的路径名访问函数

分隔

  • basename() 去掉目录路径, 返回文件名
  • dirname() 去掉文件名, 返回目录路径
  • join() 将分离的各部分组合成一个路径名
  • split() 返回 (dirname(), basename()) 元组
  • splitdrive() 返回 (drivename, pathname) 元组
  • splitext() 返回 (filename, extension) 元组

信息

  • getatime() 返回最近访问时间
  • getctime() 返回文件创建时间
  • getmtime() 返回最近文件修改时间
  • getsize() 返回文件大小(以字节为单位)

查询

  • exists() 指定路径(文件或目录)是否存在
  • isabs() 指定路径是否为绝对路径
  • isdir() 指定路径是否存在且为一个目录
  • isfile() 指定路径是否存在且为一个文件
  • islink() 指定路径是否存在且为一个符号链接
  • ismount() 指定路径是否存在且为一个挂载点
  • samefile() 两个路径名是否指向同个文件

  • os.path.isdir(name):判断name是不是一个目录,name不是目录就返回false

  • os.path.isfile(name):判断name是不是一个文件,不存在name也返回false
  • os.path.exists(name):判断是否存在文件或目录name
  • os.path.getsize(name):获得文件大小,如果name是目录返回0L
  • os.path.abspath(name):获得绝对路径
  • os.path.normpath(path):规范path字符串形式
  • os.path.split(name):分割文件名与目录(事实上,如果你完全使用目录,它也会将最后一个目录作为文件名而分离,同时它不会判断文件或目录是否存在)
  • os.path.splitext():分离文件名与扩展名
  • os.path.join(path,name):连接目录与文件名或目录
  • os.path.basename(path):返回文件名
  • os.path.dirname(path):返回文件路径

os模块中的文件操作:

  • os 模块属性
  • inesep 用于在文件中分隔行的字符串
  • sep 用来分隔文件路径名的字符串
  • pathsep 用于分隔文件路径的字符串
  • curdir 当前工作目录的字符串名称
  • pardir (当前工作目录的)父目录字符串名称

    1. 重命名:os.rename(old, new)
    2. 删除:os.remove(file)
    3. 列出目录下的文件:os.listdir(path)
    4. 获取当前工作目录:os.getcwd()
    5. 改变工作目录:os.chdir(newdir)
    6. 创建多级目录:os.makedirs(r”c:\python\test”)
    7. 创建单个目录:os.mkdir(“test”)
    8. 删除多个目录:os.removedirs(r”c:\python”) #删除所给路径最后一个目录下所有空目录。
    9. 删除单个目录:os.rmdir(“test”)
    10. 获取文件属性:os.stat(file)
    11. 修改文件权限与时间戳:os.chmod(file)
    12. 执行操作系统命令:os.system(“dir”)
    13. 启动新进程:os.exec(), os.execvp()
    14. 在后台执行程序:osspawnv()
    15. 终止当前进程:os.exit(), os._exit()
    16. 分离文件名:os.path.split(r”c:\python\hello.py”) –> (“c:\python”, “hello.py”)
    17. 分离扩展名:os.path.splitext(r”c:\python\hello.py”) –> (“c:\python\hello”, “.py”)
    18. 获取路径名:os.path.dirname(r”c:\python\hello.py”) –> “c:\python”
    19. 获取文件名:os.path.basename(r”r:\python\hello.py”) –> “hello.py”
    20. 判断文件是否存在:os.path.exists(r”c:\python\hello.py”) –> True
    21. 判断是否是绝对路径:os.path.isabs(r”.\python\”) –> False
    22. 判断是否是目录:os.path.isdir(r”c:\python”) –> True
    23. 判断是否是文件:os.path.isfile(r”c:\python\hello.py”) –> True
    24. 判断是否是链接文件:os.path.islink(r”c:\python\hello.py”) –> False
    25. 获取文件大小:os.path.getsize(filename)
    26. *:os.ismount(“c:\”) –> True
    27. 搜索目录下的所有文件:os.path.walk()

shutil模块对文件的操作:

  1. 复制单个文件:shultil.copy(oldfile, newfle)
  2. 复制整个目录树:shultil.copytree(r”.\setup”, r”.\backup”)

临时文件的操作:

  1. 创建一个唯一的临时文件:tempfile.mktemp() –> filename
  2. 打开临时文件:tempfile.TemporaryFile()

内存文件(StringIO和cStringIO)操作

[4.StringIO] #cStringIO是StringIO模块的快速实现模块
1. 创建内存文件并写入初始数据:f = StringIO.StringIO(“Hello world!”)
2. 读入内存文件数据:print f.read() #或print f.getvalue() –> Hello world!
3. 想内存文件写入数据:f.write(“Good day!”)
4. 关闭内存文件:f.close()

```
import os
import os.path
import unittest
import time
import pygame

class PyFileCommonOperatorTest(unittest.TestCase):
    def __init__(self):
        """constructor"""

    def test01(self):
        print os.linesep
        print os.sep
        print os.pathsep
        print os.curdir
        print os.pardir
        print os.getcwd()
        print 'unittest here'


if __name__ == "__main__":
    t = PyFileCommonOperatorTest()
    t.test01()

你可能感兴趣的:(Python文件操作)