3.python学习笔记:python对文件的操作

来源:互联网 发布:电脑windows怎么激活 编辑:程序博客网 时间:2024/06/11 18:33

python对文件的操作


输出文件

创建一个新的文件,可以传入open的两个参数:待创建文件的外部名称和模式字符串w。将数据存入文件则调用文件对象的write方法,向其传入要存储数据的字符串,然后调用close方法关闭文件。
示例如下:

//open、write#!/usr/bin/env python#-*-coding:utf-8-*-import sys, ossrc_file = sys.argv[1]des_file = sys.argv[2]def get_content(filename):    file_t = content = open(filename, "r")    content = file_t.read()    file_t.close()    return contentdef write_file(filename, content):    file_t = open(filename, "w")    file_t.write(content)    file_t.close()if __name__ == '__main__':    src = get_content(src_file)    print src    des = write_file(des_file, src)    print get_content(des_file)

注意:
1.在w模式下打开文件,如果文件不存在,python会创建这个文件,如果文件已经存在,则会删除文件当前的内容
2.上面用到的文件close方法是用来确定文加年内容并释放系统资源的,正常情况下,当解释其对文件对象进行垃圾回收时,文件会被自动关闭,或者python程序结束时所有打开的文件也会被关闭。

#写入临时对象open('/mnt/haha.txt', 'w').write('hello, world')    #写入临时对象open('/mnt/haha.txt', 'r').read()    #从临时对象中读取

3.有时候打开文件操作也是一件有可能失败的事情,所以我们经常使用捕获异常的机制来处理文件的操作:

#捕获异常my_file = open('/mnt/test_file', 'w')try:    my_file.write('this is the test file!\n')finally:    my_file.close()

在出现异常的时候,程序会执行finally语句块的内容,即关闭文件。
但我们更加推荐with语句,这使异常的处理变得更加方便,代码如下:

with open('/mnt/test_file', 'w') as my_file:    my_file.write('this is the test file!\n')

上述文件的打开模式为w,这样每次都会把之前的内容给删除掉,如果我们指向在已存在的文件末尾进行追加,则需要修改open的打开模式为a。

//追加文本with open('/mnt/test_file', 'a') as my_file:    my_file.write('this is apending content!\n')

输入文件

从外部文件中读取数据和写入数据一样简单,有多种方式可以对文件进行读取。关于文本读取的方式有多种:

file.read()   #返回一个字符串,它包含存在文件中的所有字符file.read(N)  #返回一个字符串,它包含文件中接下来的N个字符file.readline()    #读取下一个\n之前的内容并返回一个行字符串file.readlines()    #读取整个文件并返回一个行字符串列表

注意:
1.read()和readlines()把整个文件一次加载到内存,通常运行速度比较快,但是对于单性文件的读取将会占用大量内存。


使用迭代器读取行

在较早的版本中,借助for循环来逐行读取文件的传统做法是将文件读入列表,然后逐条读取。

#早期做法my_file = open('/mnt/test_file', 'r')for line in my_file.readlines():    print line

这个文件行迭代器不是一次性把整个文件加载到行列表,因此在处理大型文本文件时较为节省空间。

#使用迭代器进行处理my_file = open('/mnt/test_file', 'r')for line in my_file:    print (line, end='')

二进制和文本文件

python也可以打开并处理包含二进制数据的文件—jpeg图像、音频、视频和打包的二进制数据。

//打开视频文件mv_file = open('/var/ftp/pub/我是谁:没有绝对安全的系统.BD1280高清德语中字.mp4', 'rb')mv_file.read()

注意:把二进制文件的信息打印出来是没有太多意义的。


遍历目录树

遍历目录的方式

在python中我们可以使用3种方式来扫描目录:

1.使用os.popen运行shell列表命令;
2.使用glob.glob进行文件名模式匹配;
3.使用os.listdir得到目录的列表;

#使用 os.popenimport osfor line in os.popen('ls /root/Desktop/'):   print(line[:-1])

os.popen的缺点很明显,它需要使用平台专用的shell命令,在linux和windows的下的shell命令是不同的。而且需要启动一个独立的程序,从而导致性能下降。

使用glob可以得到文件的绝对路径

#使用globimport globfor file in glob.glob('/root/Desktop/'):    print file

listdir则得到的是原始的基本文件名

#使用listdirimport osfor file in os.listdir('/root/Desktop/'):    print file

遍历目录树

上述的例子中都是对单一的目录做遍历,那么如果目录中嵌套着更多的目录,我们需要怎么解决这个问题?

我们可以通过编写递归程序来遍历目录树,也可以使用python os模块的内建目录树遍历工具。

1.使用os.walk方式

#!/usr/bin/env python#-*-coding:utf-8-*-import osdef get_pdf_file(dirname):    matchs = []    for (dirname, dirshere, fileshere) in os.walk(dirname):        for filename in fileshere:            if filename.endswith('.pdf'):                pathname = os.path.join(dirname, filename)                matchs.append(pathname)    return matchsdef show_file(files):    for name in files:        print nameif __name__ == '__main__':    pdf_file = get_pdf_file('/root/Desktop/')    show_file(pdf_file)

上述列子获取了指定目录下的所有pdf文件,对该目录下的子目录也进行了同样的处理。

2.使用os.listdir递归遍历

使用os.listdir手动生成文件路径,并以递归方式调用自身

#!/usr/bin/env python#-*-coding:utf-8-*-import osimport sysdef my_list(dirname):    print ('[' + dirname + ']')    for file1 in os.listdir(dirname):        path = os.path.join(dirname, file1)        if not os.path.isdir(path):            print path        else:            my_list(path)if __name__ == '__main__':    my_list('/root/Desktop/')

小结:

上述内容是关于python语言对于文件和目录的相关操作,希望大家可以掌握。

2 0