标签:
打开文件
默认是以r,只读模式打开的
>>> f = open("/test/demo.txt") >>> for line in f: ... print line, #后面加一个逗号,就去掉了原来默认增加的 \n 了,少了空行。 ... hello python
写文件
"w":以写方式打开文件,可向文件写入信息。如文件存在,则清空该文件,再写入新内容,如果不存在则创建文件
>>> f1 = open("/test/demo.txt","w") >>> f1.write("hello,python") >>> f1.close()
"a":以追加模式打开文件(即一打开文件,文件指针自动移到文件末尾),如果文件不存在则创建
>>> f2 = open("/test/demo22.txt","a") >>> f2.write("I like C\n") >>> f2.close()
使用 with
这里就不用 close()了。而且这种方法更有 Python 味道,或者说是更符合 Pythonic 的一个要求。
>>> with open("/test/demo.txt","a") as f: ... f.write("Hello World") ... >>> with open("/test/demo.txt","r") as f:
... print f.read()
...
hello,pythonHello World
获取文件状态
>>> import os >>> file_stat = os.stat("131.txt") >>> file_stat posix.stat_result(st_mode=33188, st_ino=7077890, st_dev=64768, st_nlink=1, st_uid=0, st_gid=0, st_size=14, st_atime=1473126833, st_mtime=1473126819, st_ctime=1473126819) >>> file_stat.st_ctime 1473126819.3923097 >>> import time #引入time模块,转换时间 >>> time.localtime(file_stat.st_ctime) time.struct_time(tm_year=2016, tm_mon=9, tm_mday=6, tm_hour=9, tm_min=53, tm_sec=39, tm_wday=1, tm_yday=250, tm_isdst=0)
read/readline/readlines
>>> f = open("you.md")
>>> content = f.read()
>>> content
‘You Raise Me Up \nWhen I am down and, oh my soul, so weary; \nThen troubles come and my heart burdened be; \n‘
>>> print content
You Raise Me Up
When I am down and, oh my soul, so weary;
Then troubles come and my heart burdened be;
>>> f.close()
>>> f.readline()
‘You Raise Me Up \n‘
>>> f.readline()
‘When I am down and, oh my soul, so weary; \n‘
>>> f.readline()
‘Then troubles come and my heart burdened be; \n‘
>>> f.readline()
‘‘
>>> f.close()
用循环语句完成对整个文件的读取
#/usr/bin/env python #coding=utf-8 f = open("/python/you.md") while True: line = f.readline() if not line: #到 EOF,返回空字符串,则终止循环 break print line, #注意后面的逗号,去掉 print 语句后面的 ‘\n‘,保留原文件中的换行 f.close()
>>> f = open("you.md")
>>> content = f.readlines()
>>> content
[‘You Raise Me Up \n‘, ‘When I am down and, oh my soul, so weary; \n‘, ‘Then troubles come and my heart burdened be; \n‘]
>>> for line in content: #使用循环取出
... print line,
...
You Raise Me Up
When I am down and, oh my soul, so weary;
Then troubles come and my heart burdened be;
读很大的文件
如果文件太大,就不能用read() 或者readlines() 一次性将全部内容读入内存,可以使用 while 循环和readlin() 来完成这个任务。
此外,还有一个方法:fileinput 模块
>>> import fileinput >>> for line in fileinput.input("you.md"): ... print line, ... You Raise Me Up When I am down and, oh my soul, so weary; Then troubles come and my heart burdened be;
还有一种方法更常用
>>> f = open("you.md") >>> for line in f: ... print line, ... You Raise Me Up When I am down and, oh my soul, so weary; Then troubles come and my heart burdened be; >>> f.close()
之所以能够如此,是因为 file 是可迭代的数据类型,直接用 for 来迭代即可。
seek
这个函数的功能就是让指针移动。特别注意,它是以字节为单位进行移动的。比如:
>>> f = open("you.md") >>> f.readline() ‘You Raise Me Up \n‘ >>> f.readline() ‘When I am down and, oh my soul, so weary; \n‘ >>> f.seek(0) >>> f.readline() ‘You Raise Me Up \n‘
此时指针所在的位置,还可以用tell() 来显示,如
>>> f.tell()
17
标签:
原文地址:http://www.cnblogs.com/zydev/p/5843201.html