day14 文件操作

第十四天 文件操作

一、数据持久化

1、数据持久化

"""
计算机存储空间分为:运行内存和磁盘两种。
程序中产生的数据默认都是保存在运行内存中,存储在运行内存中的数据在程序结束后会自动销毁。
如果将数据存储到磁盘中,那么数据除非手动删除或者磁盘损坏,否则会一直存在。磁盘存储数据的基本单位是文件。

数据持久化指的就是将程序中的数据以文件的形式保存到磁盘中。
"""
a = 100
b = ['abc', 'mn', 'xy']

2、常见数据持久化工具

"""
数据库(.db、.sqlite)、json文件(.json)、xml文件(.xml)、plist文件(.plist)、普通文本文件(.txt)、
excel文件(.xls、.xlsx)、csv文件(.csv)
"""

二、文件操作 - 操作文件内容

1、文件操作基本流程


# 打开文件  ->  操作文件(读操作、写操作)  -> 关闭文件

2、打开文件

# 2. 打开文件
"""
open(file, mode='r',*,encoding=None)     -  以指定方式打开指定文件,返回一个文件对象
1) 参数file  -  字符串,文件路径,用来确定要打开的是哪个文件
                a.绝对路径:文件在计算机中的全路径
                b.相对路径:用.表示当前目录(当前目录指的当前代码文件所在的目录)  - ./可以省略
                          用..表示当前目录的上层目录
                注意:使用相对路径的时候必须保证文件在工程中
                
2) 参数mode  -  字符串,文件打开方式; 决定打开文件后能做什么(读or写?),决定操作文件的时候数据的类型(二进制or字符串?)  
                第一组值: -  决定打开文件后能做什么(读or写?)
                         r  -   只读
                         w  -   只写;打开的时候会清空原文件(覆盖)
                         a  -   只写;打开的时候保留原文件内容(追加)
                第二组值: -  决定操作文件的时候数据的类型
                         t  -  字符串(str)
                         b  -  二进制(bytes)
                         
                注意:a. 给mode赋值的时候必须在两组值中每一组选一个,如果第二组不选就相当于选的't'
                'r' == 'rt' == 'tr'
                'w' ==  'wt' == 'tw'
                'rb' == 'br'
                b. 所有的文件都可以以b的方式打开,但是只有文本文件才能用t打开
                c. 以读的方式打开一个不存在的文件程序会报错;
                   以写的方式打开一个不存在的文件程序不会报错,并且会自动创建这个文件。
                   
3) encoding  -  文本文件编码方式,使用的时候一般设置成 'utf-8'
                注意:a. 一般不需要设置这个值,默认和电脑默认编码方式一致。
                     b. 如果是以b的方式打开文件,一定不能给encoding赋值
                     
                         
"""

2.1 文件路径

open(r'/Users/yuting/lessons/Python2107/01语言基础/day14-文件操作/files/a.txt', encoding='utf-8')
open(r'./files/a.txt', encoding='utf-8')
open(r'./01review.py', encoding='utf-8')

open('../day14-文件操作/files/a.txt')

2.2文件的打开方式

# a. r - 只读
# f = open('./files/a.txt', 'r')
# f.read()
# f.write('MAM')        # io.UnsupportedOperation: not writable

# b. w - 只写并且清空原文件
# f = open('./files/a.txt', 'w')
# f.write('ABC')
# # f.read()      # io.UnsupportedOperation: not readable

# c. a - 只写,保留原文件内容
# f = open('./files/a.txt', 'a')
# f.write('Hello!')
# # f.read()      # io.UnsupportedOperation: not readable

# d. t - 字符串
# f = open('./files/a.txt', 'rt')
# result = f.read()
# print(type(result))     # <class 'str'>

# e. b - 二进制
# f = open('./files/a.txt', 'rb')
# result = f.read()
# print(type(result))     # <class 'bytes'>


# open('./files/b.txt', 'r')        # FileNotFoundError: [Errno 2] No such file or directory: './files/b.txt'
# open('./files/c.txt', 'w')
# open('./files/d.txt', 'a')

3、操作文件

3.1读操作

1)读操作
文件对象.read()     -       从读写位置开始读到文件结尾(读写位置默认在文件开头, 一次读完)
文件对象.readline()     -   从读写位置开始到一行结束(一次读一行,只支持文本文件)
# 1)读操作
f = open('files/a.txt', 'r')
result = f.read()
print(result)

print('----------------------------------------------')
f.seek(0)       # 将读写位置移动到文件开头
result = f.read()
print(result)

print('----------------------------------------------')
f.seek(0)

# result = f.readline()
# print(result)
#
# result = f.readline()
# print(result)

result = f.read().split('\n')
print(result)

3.2写操作

f = open('./files/a.txt', 'a')
nums = [10, 20, 30, 40]
f.write(str(nums))

4、关闭文件

"""
文件对象.close()
"""
def func1():
    f2 = open('files/a.txt')


result = open('files/a.txt').read()
print(type(result))

f = open('files/a.txt')
print(type(f))
result = f.read()
f.close()
上一篇:Git 使用 Window Terminal 的配置


下一篇:Flask项目基本流程