码迷,mamicode.com
首页 > 编程语言 > 详细

Python学习笔记9—文件

时间:2016-09-06 12:17:41      阅读:294      评论:0      收藏:0      [点我收藏+]

标签:

打开文件

技术分享

默认是以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

  • read:如果指定了参数 size,就按照该指定长度从文件中读取内容,否则,就读取全文。被读出来的内容,全部塞到一个字符串里面。这样有好处,就是东西都到内存里了,随时取用,比较快捷;“成也萧何败萧何”,也是因为这点,如果文件内容太多了,内存会吃不消的。文档中已经提醒注意在“non-blocking”模式下的问题
>>> 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()
  •  readline:那个可选参数 size 的含义同上。它则是以行为单位返回字符串,也就是每次读一行,依次循环,如果不限定 size,直到最后一个返回的是空字符串,意味着到文件末尾了(EOF)。
>>> 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()
  • readlines:size 同上。它返回的是以行为单位的列表,即相当于先执行readline() ,得到每一行,然后把这一行的字符串作为列表中的元素塞到一个列表中,最后将此列表返回。
>>> 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

 

Python学习笔记9—文件

标签:

原文地址:http://www.cnblogs.com/zydev/p/5843201.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!