Python——文件操作

1. 文件操作基础

(1)文件路径

    绝对路径:是从盘符开始的路径,形如 C:\windows\system32\cmd.exe

    相对路径:是从当前路径开始的路径,假如当前路径为C:\windows,要描述上述路径,只需输入system32\cmd.exe。
            实际上,严格的相对路径写法应为.\system32\cmd.exe。其中,.表示当前路径,在通道情况下可以省略,只有在特殊的情况下不能省略。

    

(2)编码方式

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

(3)操作方式

    只读、只写、追加、读写【r+,最常用】、写读...

2. 操作方式详解

  bytes用于查看非文字类的文件、上传下载储存文件

  下面所有例子都由相对路径来描述:

(1)只读:r   rb

#正常:r   只读

print('--------相对路径:只读----------')
f = open('文件',mode='r',encoding='utf-8')    #文件:你好呀
content = f.read()
print(content)  #你好呀
f.close()

#bytes:rb

print('--------相对路径:只读----------')
f = open('文件',mode='rb')  
content = f.read()
print(content)  #b'\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x91\x80'
f.close()

(2)只写:w  wb  

#正常:w   只写

#文件不存在,则创建完了再写
print('--------相对路径:只写----------')
f = open('file1',mode='w',encoding='utf-8') #file1不存在
f.write("halou")    #halou
f.close()
#文件存在,先删除原文件内容,再写
f = open('file1',mode='w',encoding='utf-8') #file存在,内容:halou
f.write("辛辰辰")  #辛辰辰
f.close()

#bytes:wb
# 文件不存在,则创建完了再写
print('--------相对路径:只写----------')
f = open('file2',mode='wb')
f.write("哈喽halou123".encode('utf-8'))  #需要对写的内容进行编码encode,文件内容:哈喽halou123
f.close()
#文件存在,先删除原文件内容,再写
f = open('file2',mode='w',encoding='utf-8') #file存在,内容:halou
f.write("辛辰辰")  #辛辰辰
f.close()

(3)追加:a   ab

#正常:a   追加
print('--------相对路径:追加----------')
f = open('file1',mode='a',encoding='utf-8') #file1:辛辰辰
f.write('追加追加')     #辛辰辰追加追加
f.close()

#bytes:ab
print('--------相对路径:追加----------')
f = open('file2',mode='ab') #file2:辛辰辰
f.write('zai追加'.encode('utf-8'))    #辛辰辰zai追加
f.close()

(4)读写:r+    r+b 

#正常:r+  读写【最常用】
print('--------相对路径:读写----------')
f = open('file',mode='r+',encoding='utf-8')  #file:python9
print(f.read()) #python9
f.write('辛辰辰')  #python9辛辰辰
f.close()

print('--------相对路径:写读----------')
f = open('file',mode='r+',encoding='utf-8')  #file:python9
f.write('xcc')  #file:xcchon9
print(f.read()) #hon9
f.close()

#bytes:r+b
print('--------相对路径:读写----------')
f = open('file',mode='r+b') #file:python9
print(f.read()) #b'python9'
f.write('辛辰辰'.encode('utf-8'))  #file:python9辛辰辰
f.close()

print('--------相对路径:写读----------')
f = open('file',mode='r+b') #file:python9
f.write('xcc'.encode('utf-8'))  #file:xcchon9
print(f.read()) #b'hon9'
f.close()

(5)写读:w+  w+b

#正常:w+  写读(先清除,再写)
print('--------相对路径:写读----------')
f = open('file',mode='w+',encoding='utf-8')  #file:python9
f.write('辛辰辰')  #辛辰辰
print(f.read())   #
f.close()

#bytes:w+b
print('--------相对路径:写读----------')
f = open('file',mode='w+b')  #file:python9
f.write('辛辰辰'.encode('utf-8'))  #辛辰辰
print(f.read())    #b''
f.close()

(6)追加:a+  a+b

#正常:a+  追加
print('--------相对路径:追加----------')
f = open('file',mode='a+',encoding='utf-8')  #file:python9
f.write('辛辰辰')  #python9辛辰辰
print(f.read()) #
f.seek(0)   #调整光标位置到开始的地方
print(f.read()) #python9辛辰辰
f.close()

#bytes:a+b
print('--------相对路径:追加----------')
f = open('file',mode='a+b')  #file:python9
f.write('辛辰辰'.encode('utf-8'))  #python9辛辰辰
print(f.read()) #b''
f.seek(0)   #调整光标位置到开始的地方
print(f.read()) #b'python9\xe8\xbe\x9b\xe8\xbe\xb0\xe8\xbe\xb0'
f.close()

(7)修改文件

  文件是不能修改的,要想修改就先删除原文件再重命名备份文件

#修改文件
with open('班主任',encoding='utf-8') as f1,open('班主任.bak','w',encoding='utf-8')as f2:
    for line in f1:
        if '欣儿' in line:
            line = line.replace('欣儿','阿娇')
        # 写文件
        f2.write(line)
# 先删除原文件再重命名备份文件,即修改成功
import os
os.remove('班主任')        #删除文件
os.rename('班主任.bak','班主任')  #重命名文件

3. 功能详解

(1)read

  读出来的都是字符,一个字母/汉字

print('--------功能详解----------')
# f = open('file',mode='r+',encoding='utf-8') #file:pythonpythonpython
f = open('file',mode='r+',encoding='utf-8') #file:你好你好你好
content = f.read(3) #read读出来的都是字符,一个字母/汉字
# print(content)  #pyt
print(content)  #你好你
f.close()

(2)seek

  按照字节定光标的位置(一个汉字3字节,一个英文字母1字节)

print('--------功能详解----------')
# f = open('file',mode='r+',encoding='utf-8') #file:PYthonpyTHonpythON
f = open('file',mode='r+',encoding='utf-8') #file:你好呀辛辰
f.seek(3)   #光标:按照字节定位置
content = f.read()
# print(content)  #honpyTHonpythON:光标位置再三个字节之后(utf-8中:一个字母一个字节)
print(content)  #好呀辛辰:光标位置再三个字节之后(utf-8中:一个汉字三个字节)
f.close()

(3)tell

  监测光标的位置

print('--------功能详解----------')
f = open('file',mode='r+',encoding='utf-8') #file:PYthonpyTHonpythON
# f = open('file',mode='r+',encoding='utf-8') #file:你好呀辛辰
f.seek(3)
f.tell()    #监测光标的位置
print(f.tell())  #3
# print(f.read()) #从第三个字节后面开始读取:好呀辛辰
print(f.read()) #从第三个字节后面开始读取:honpyTHonpythON
f.close()

  应用举例:

f = open('file',mode='a+',encoding='utf-8')  #file:python9
f.write('辛辰辰')  #python9辛辰辰
count = f.tell()
f.seek(f.tell()-3)
print(f.read()) #辰(后三个字节表示一个汉字)
f.close()

#读取文件file中的从第3个字节开始的连续5个字节的内容
f = open('file',mode='a+',encoding='utf-8')  #file:python9辛辰辰
f.seek(2)
print(f.read(5)) #thon9(python9辛辰辰)
f.close()

(4)with open

  使用with open打开文件,可以不用写 f.close()

#打开一个文件
with open('file',mode='r+',encoding='utf-8') as f:
    print(f.read())  #这是file1文件
#打开多个文件
with open('file1',mode='r+',encoding='utf-8') as f1,open('file2',mode='w+',encoding='utf-8') as f2:
    print(f1.read())    #这是file1文件
    f2.write('哈喽')  #file2:哈喽
    print(f2.read())    #空:w+是先清除再写

(5)others

f = open('file',mode='r+',encoding='utf-8')
f.readable()    #是否可读
print(f.readable()) #True
line = f.readline()    #读一行
print(line) #这是file文件1
lines = f.readlines()    #把每一行当成列表中的一个元素,添加到list中
print(lines)   #['这是file文件2\n', '这是file文件3\n', '这是file文件4\n', '这是file文件5']
# f.truncate(5)   #截取前五位
f.close()

 

posted @ 2018-09-15 21:11  xc_718  阅读(116)  评论(0编辑  收藏  举报