【python学习笔记】文件的创建、读取和写入

本博客参考了如何使用python打开及读写文件(基础篇)

文件创建

创建空文件

os.mknod("test.txt")

该方法不适用于Windows系统

直接打开一个文件,如果文件不存在则创建文件

import os

file = open("test01.txt",'w')
file.close()
with open("test01.txt", 'w', encoding="utf-8") as f:
    f.write("test")
with open("test01.txt", 'r', encoding="utf-8") as f:
    print(f.read())
test
#封装的文件创建方法
def text_create(name):
    #此处是文件保存位置,默认为项目根目录
    savepath = ""
    filename = savepath + name + '.txt'
    filename = name + '.txt'
    file = open(filename, 'w')
    file.close()

text_create('test')

文件写入

这里的写入我指的不只是write操作

write

write操作只能把文件的内容全部清空后重写写入

with open("test.txt", 'w', encoding="utf-8") as f:
    f.write("the 1st line\n")
#读取操作
with open("test.txt", 'r', encoding="utf-8") as f:
    print(f.read())
print("-------------------")
with open("test.txt", 'w', encoding="utf-8") as f:
    f.write("the 2nd line\n")

with open("test.txt", 'r', encoding="utf-8") as f:
    print(f.read())
the 1st line

-------------------
the 2nd line

append

append操作是在原有基础上追加内容

with open("test.txt", 'a', encoding="utf-8") as f:
    f.write("the 3rd line\n")
#读取操作
with open("test.txt", 'r', encoding="utf-8") as f:
    print(f.read())
the 2nd line
the 3rd line

删除内容

with open("test01.txt", 'r', encoding="utf-8") as f:
    print(f.read())
    
#f.truncate(n)从头开始,第n个字符后截断并清除
#即显示前n个字符
with open("test01.txt", 'a', encoding="utf-8") as f:
    f.truncate(2)

with open("test01.txt", 'r', encoding="utf-8") as f:
    print(f.read())
    print("-------------")
    
#清除文件内容
with open("test01.txt", 'w', encoding="utf-8") as f:
    f.truncate()

with open("test01.txt", 'r', encoding="utf-8") as f:
    print(f.read())
test
te
-------------

#删除文件
os.remove("test01.txt")

#检验文件是否存在
os.path.exists("test01.txt")
False

文件读取

open()+close()

#打开文件
f = open('test.txt','r',encoding = 'utf-8')

#再使用read()方法,查看文件里的内容:
print(f.read())

#关闭文件
f.close()
the 2nd line
the 3rd line

注意

  • 当未指定文件编码格式时,如果文件编码格式与当前默认的编码格式不一致,那么文件内容的读写将出现错误,在python3下,可以通过encoding参数指定编码方式。
  • 结尾一定要使用close()来关闭文件。
  • 当读写文件本身有错误时,即使使用close()也可能会出现文件无法正常关闭的现象。

with open()

with 的作用相当于调用close()方法,文件会自动关闭,这种方法的安全系数更高,同时也避免了有些时候忘记关闭文件的毛病。

with open('test.txt','r',encoding = 'utf-8') as f:
    print(f.read())
the 2nd line
the 3rd line

with open('test.txt','a',encoding = 'utf-8') as f:
    f.write("test\ntest2\ntest3\n")
with open('test.txt','r',encoding = 'utf-8') as f:
    print(f.read())
the 2nd line
the 3rd line
test
test2
test3

几种读取的方式

#读取全部
with open('test.txt','r',encoding = 'utf-8') as f:
    print(f.read())
    print(f.tell()) #打印指针位置,read()操作之后指针在末尾
    f.seek(0) #移动指针(0-文件头,默认值;1-当前位置;2-文件尾)
    print(f.tell())
    print("-----------")

#读取第一行
with open('test.txt','r',encoding = 'utf-8') as f:
    print(f.readline())
    print("-----------")
    
#读取前n个字符,这里n=2   
with open('test.txt','r',encoding = 'utf-8') as f:
    print(f.readline(2))
    print("-----------")
    
#一次读取所有内容并按行返回list
with open('test.txt','r',encoding = 'utf-8') as f:
    print(f.readlines())
    print("-----------")
the 2nd line
the 3rd line
test
test2
test3

48
0
-----------
the 2nd line

-----------
th
-----------
['the 2nd line\n', 'the 3rd line\n', 'test\n', 'test2\n', 'test3\n']
-----------
posted @ 2021-02-23 01:00  ryukirin  阅读(725)  评论(0编辑  收藏  举报