Skip to main content
 Web开发网 » 编程语言 » Python语言

Python常见文件如何操作?

2021年11月27日6550百度已收录

  # -*-coding:utf8 -*-

''''' 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 模块属性linesep 用于在文件中分隔行的字符串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")

3。删除整个目录树:shultil。rmtree(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()

view plainprint?

view plainprint?

#读文件的写法:#读文本文件:input = open('data', 'r')#第二个参数是默认的,可以不加#读二进制文件:input = open('data', 'rb')

#读取所有文件内容:open('xxoo。

  txt')。read()

#读取固定字节open('abinfile', 'rb')。read(100)

#读每行file_object。readlines()。

评论列表暂无评论
发表评论
微信