Day8

文件操作

参数:1,、文件路径

    2、编码方式。encode

    3、执行动作(打开方式):只读,只写,追加,读写,写读。。。

    读:

      ****r  只读

      1、文件以什么编码方式存储的,就以什么编码方式打开

        #编码不一致时:unicodeDecodeerror:‘utf-8’ codec can't decode byte 0xb7 in position 0: invalid start byte

      2、文件路径

        绝对路径:从根目录开始,一级一级查找直到找到文件。D:\t1.txt

        相对路径:在同一个文件夹下,直接写文件名即可

        /U//UU

        #'c:\log.txt'

          第一种:r'C:\log.txt'

          第二种:C:\\log.txt

            as.txt

******五种方式:

#1:全部读出来f.read()

         f= open('log',encoding='utf-8')

        content = f.read()

        print(content,type(content))

        f.close()

#2:一行一行的读

        f = open('log',encoding='utf-8')

        print(f.readline())

        print(f.readline())

        print(f.readline())

        print(f.readline())

        f.close()

#3:将原文件的每一行作为一个列表的元素

        f = open('log',encoding='utf-8')

        print(f.readlines())

        f.close()

#4:读取一部分read(n)

        在r模式下,read(n)按照字符去读取

        在rb模式下,read(n)按照字节去读取

        

f=open('log',encoding='utf-8')
print(f.read(3))
f.close()
f= open('log',mode='rb')
content=f.read(4)
print(content)
f.close()

#5:循环读取

f=open('log',encoding='utf-8')
for i in f:
    print(i.strip())
f.close()

#非文字类的文件时,用rb 只读,以bytes类型读取

f=open('log1',mode='rb')
content=f.read()
print(content)
f.close()

#r+ 先读,后追加 一定要先读后写

f= open('log',encoding='utf-8',mode='r+')
content = f.read()
print(content)
f.write('aaa')
f.close()

 

#没有文件 创建一个文件写入内容   w

f=open('log2',encoding='utf-8',mode='w')

f.write('安师大会山东矿机哈四大皆空干啥风格')

f.close()

#有文件,将原文件内容清空,在写入内容

f= open('log1',encoding='utf-8',mode='w')

f.write('666')

f.close()

#wb

f = open('log',mode='wb')
f.write('老男孩教育'.encode('utf-8'))
f.close()

#w+ 先写后读

f = open('log',encoding='utf-8',mode='w+')
f.write('中国')
print(f.tell())  #按字节去读光标位置
f.seek(3)  #按照字节调整光标位置
print(f.read())
f.close()

#w+b

 

#a+ 追加读

f=open('log',encoding='utf-8',mode='a+')
f.write('BBB')
content= f.read()
print(content)
f.close()

#a+b

 

#其他方法

f= open('log',encoding='utf-8')
print(f.read())
print(f.writable())
f.close()
f = open('log',encoding='utf-8',mode='a')
f.truncate(7) # 按字节对原文件截取
f.close()
#功能一:自动关闭文件句柄。
#功能二:一次性操作多个文件句柄。
with open('log',encoding='utf-8') as f:
    print(f.read())
with open('log1',encoding='utf-8') as f1:
    print(f1.read())
with open('log',encoding='utf-8') as f1,\
    open('log1',encoding='utf-8') as f2:
    print(f1.read())
    print(f2.read())
# 1,将原文件读取到内存。
# 2,在内存中进行修改,形成新的内容。
# 3,将新的字符串写入新文件。
# 4,将原文件删除。
# 5,将新文件重命名成原文件。
import os
with open('log',encoding='utf-8') as f1,\
    open('log.bak',encoding='utf-8',mode='w') as f2:
    content = f1.read()
    new_content = content.replace('alex','SB')
    f2.write(new_content)
os.remove('log')
os.rename('log.bak','log')
import os
with open('log',encoding='utf-8') as f1,\
    open('log.bak',encoding='utf-8',mode='w') as f2:
    for i in f1:
        new_i = i.replace('SB','alex')
        f2.write(new_i)
os.remove('log')
os.rename('log.bak','log')

 写:

*w

#没有文件,创建一个文件写入内容
# f = open('log1',encoding='utf-8',mode='w')
# f.write('儿科王金发;剪短发了肯定撒就废了;就')
# f.close()
#有文件,将原文件内容清空,在写入内容。
# f = open('log1',encoding='utf-8',mode='w')
# f.write('666')
# f.close()
wb
# wb
# f = open('log',mode='wb')
# f.write('老男孩教育'.encode('utf-8'))
# f.close()
w+
w+b

追加:*a

ab

a+
a+b

操作方法:
read
read(n)
readline()
readlines()
tell
seek
truncate
writable()
readable()

posted @ 2018-03-27 18:49  Gentleman王  阅读(95)  评论(0编辑  收藏  举报