Python学习笔记七:文件操作

文件操作

对照一个word文件的操作方式,来体会文件操作的内容

打开文件:f=open(“file”),提示编码错误,windows默认是GBK

f=open(“file”,encoding=”utf-8)

输出全部内容:print(f.read())

文件打开模式:默认为”r”,只读;”w”,写,创建新文件写,原文件内容被覆盖; “a”,append,附加在文件最后

读、写只能有一种操作

关闭文件:f.close()

一次读取一行:f.readline()

读取文件并打印,不打印第十行,如何写

# low loop

for index, line in enumerate(f.readlines()): #一次性将文件读入内存,如果文件特别大,呵呵。

if index==9:

    continue

print(line)

# high big

count=0

for line in f

count++

if count==10:

    continue

print(line)

 

posted @ 2017-07-28 21:42  沐雨霖森  阅读(113)  评论(0编辑  收藏  举报