码迷,mamicode.com
首页 > 其他好文 > 详细

文件操作

时间:2018-07-20 19:03:42      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:多个   好的   size   def   end   \n   就是   作文件   adl   

打开一个文件:

f=open(‘文件路径‘,‘打开方式’,‘编码方式’)

这样打开文件得到的是一个文件句柄,其中文件路径可以是绝对路径,也可以是相对路径,打开方式有w,r,a,w+,r+,a+,wb,rb,ab,wb+,,rb+,ab+,最常用的是w,r,a三种方式;编码方式,源文件以什么编码方式存储的,就以什么编码方式打开 

 

模式 可做操作 若文件不存在 是否覆盖
r 只能读 报错 -
r+ 可读可写 报错
w 只能写 创建
w+  可读可写 创建

a   只能写 创建 否,追加写
a+ 可读可写 创建 否,追加写

下面依次举例各种文件打开方式:

student.txt



Im a student
Im a police

只读:r

f=open(student.txt,mode=r,encoding=utf-8)
content=f.read()#只能读取文件内容,如果想要写入内容,则会报错
print(content)
f.close()

#输出
I‘m a student
I‘m a police

 

 

 

log

abcdefg
hijklmn
opqrstu
vwxyz

 

 

只写:w

 

f=open(log,mode=w,encoding=utf-8)
f.write(牛逼)#w模式会先清空文件内容再写入函数里的内容
f.close()

 

#输出

log:

牛逼

 

 

只追加:a

 

 

f=open(log,mode=a,encoding=utf-8)
f.write(666)#a模式会将函数中的内容追加到文件内容的末尾
f.close()

 

#输出

log:

abcdefg hijklmn opqrstu vwxyz666

 

读写:r+

#先读后写
f=open(log,mode=r+,encoding=utf-8) print(f.read()) f.write(aaa) f.close()

#控制台输出:
abcdefg
hijklmn
opqrstu
vwxyz

文件输出:

log:


abcdefg
hijklmn
opqrstu
vwxyzaaa

 

#先写后读

f=open(log,mode=r+,encoding=utf-8)
f.write(aaa)#写入完成后光标移动到aaa后面
print(f.read())
f.close()

#控制台输出
defg
hijklmn
opqrstu
vwxyz

文件输出:

aaadefg
hijklmn
opqrstu
vwxyz

 

 

写读:w+

#先写后读

f=open(log,mode=w+,encoding=utf-8)
f.write(aaa)
print(f.read())#如果不改变文件指针读就什么也读不出来
f.close()

#控制台输出
什么都没有#因为写入‘aaa’之前文件被清空,写入‘aaa’后文件指针移动‘aaa’后面,所以再想输出就是什么都没有

#文件输出

log:

aaa

 

#先读后写

f=open(log,mode=w+,encoding=utf-8)
print(f.read())
f.write(abc)
f.close()

#控制台输出
什么都没有,因为只要是先写的模式无论是只写(w)还是写读(w+)(无论先写后读还是先读后写),第一步都是清空文件

#文件输出:

log:

abc

 

追加读:a+

 

f=open(log,mode=a+,encoding=utf-8)
f.write(abc)#在文件末尾追加
f.seek(0)#将文件指针调到最开头
print(f.read())#读取整个文件内容
f.close()

#控制台输出
abcdefg
hijklmn
opqrstu
vwxyzabc

#文件输出

log:

abcdefg
hijklmn
opqrstu
vwxyzabc

 

文件操作相关方法详解:

read()方法

f=open(log,mode=r+,encoding=utf-8)
# content=f.read(3)#读出来的都是字符,可以是3个英文字母,也可以是3个中文字符
print(f.read())
f.close()

readline()方法

f=open(log,mode=r+,encoding=utf-8)
line=f.readline()#一次只读一行
print(line)
f.close()

#输出
abcdefg

readlines()方法

f=open(log,mode=r+,encoding=utf-8)
line=f.readlines()#每一行当成列表中的一个元素,添加到list中
print(line)
f.close()

#输出
[‘abcdefg\n‘, ‘hijklmn\n‘, ‘opqrstu\n‘, ‘vwxyzabc‘]

read(),readline(),readlines()方法用来读取文件内容都不好,因为不知道文件大小,若文件大过内存则会导致内存溢出,所以最好的方法是用for循环读取文件内容

f=open(log,mode=r+,encoding=utf-8)
for line in f:
    print(line)
f.close()

 

seek()方法

f=open(log,mode=r+,encoding=utf-8)
f.seek(3)#是按照字节定文件指针的位置,utf-8中一个中文三个字节,一个英文一个字节,所以若文本内容是中文,而seek里的数字不是3的倍数就会报错
print(f.read())
f.close()

tell()方法

f=open(log,mode=a+,encoding=utf-8)
f.write(chendong)
count=f.tell()#告诉你文件指针的位置,也是按照字节来定,关于文件指针都是按照字节来定
f.seek(count-8)
print(f.read())
print(count)
f.close()

 

为了防止忘记关闭文件,可以用with来操作文件:

with open(log,mode=r+,encoding=utf-8) as f1:
print(f1.readline())

还可以同时打开多个文件:

with open(log,mode=r+,encoding=utf-8) as f1,open(log2,mode=r+,encoding=utf-8) as f2:
print(f1.readline())
print(f2.readline())

 

truncate([size])方法:

注意该方法不能工作在只读(r)模式

truncate()方法用于截断,size参数是可选的,若没有size值,从文件指针后面的所有文件内容都被截断,若有size值,从文件最开始size个字符后截断,无论光标在哪

log:

abcdefg
hijklmn
opqrstu
vwxyzabc

若有size参数:

f=open(log,mode=r+,encoding=utf-8)
f.truncate(5)
print(f.read())
f.close()

#文件输出为:
abcde

若没有size参数:

f=open(log,mode=r+,encoding=utf-8)
f.seek(10)#将文件指针定位在5
f.truncate()#没有参数,则指针后面的内容全部截断
print(f.read())
f.close()

#文件输出为
abcdefg
h
#换行符占两个字节

 

若想替换一个文件中部分内容:

可以采取以下思路:

新建一个空白文件,将已经修改过的原文件内容写进去,将原文件删除,将新文件命名为原文件的名字

代码如下:

student:

Im a student
Im a police

#现在想将police替换为teacher
with open(student.txt,mode=r,encoding=utf-8) as f1,open(student.bak,mode=w,encoding=utf-8) as f2:
    for line in f1:
        if police in line:
            line=line.replace(police,teacher)
        f2.write(line)
import os
os.remove(student.txt)
os.rename(student.bak,student.txt)

 

文件操作

标签:多个   好的   size   def   end   \n   就是   作文件   adl   

原文地址:https://www.cnblogs.com/cjluchen/p/9339052.html

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