I/O操作

一.打开文件

# open(file, mode='r', buffering=- 1, encoding_=None, errors=None, newline=None, closefd=True, opener=None)
# 上面一行的注释中,‘encoding’一词会被Python解析,暂时在后面加上一个'_'以规避Python的解析
# 使用open函数来打开一个文件
# 参数:
#   file  要打开的文件的名字(路径)
# 返回值:
#   返回一个对象,这个对象就代表了当前打开的文件

# 使用相对路径
file_name_01 = 'demo_01.txt'
file_name_02 = 'file_01//demo_02.txt'
file_name_03 = '..//file_02//demo_03.txt'
file_obj_01 = open(file_name_01)  # 打开路径为'demo_01.txt'的文件
file_obj_02 = open(file_name_02)  # 打开路径为'file_01//demo_02.txt'的文件
file_obj_03 = open(file_name_03)  # 打开路径为'..//file_02//demo_03.txt'的文件
print(file_obj_01) # <_io.TextIOWrapper name='demo_01.txt' mode='r' encoding='cp936'>
print(file_obj_02) # <_io.TextIOWrapper name='file_01//demo_02.txt' mode='r' encoding='cp936'>
print(file_obj_03) # <_io.TextIOWrapper name='..//file_02//demo_03.txt' mode='r' encoding='cp936'>

# 使用绝对路径
file_name_04 = 'D:\\PythonProjects\\PythonLearning\\lesson07\\code\\demo_01.txt'
file_obj_04 = open(file_name_04)    # 打开路径为'D:\\PythonProjects\\PythonLearning\\lesson07\\code\\demo_01.txt'的文件
print(file_obj_04)  # <_io.TextIOWrapper name='D:\\PythonProjects\\PythonLearning\\lesson07\\code\\
# demo_01.txt' mode='r' encoding='cp936'>

二.关闭文件

file_name = 'demo_01.txt'
file_obj = open(file_name)

# 先对文件进行简单的读取
file_context = file_obj.read()
print(file_context) # Lock horns and battle other players in all the latest .io games. Enjoy original tit...

# 实际运用中,文件用完后,需要关闭,否则会持续占用资源
file_obj.close()

print('#'*80)

# 为了防止我们忘记关闭文件,Python提供一种语法,与捕获异常的语法进行搭配,在实际运用中用来操作文件并关闭文件
try:
    with open('demo_01.txt') as file_obj:
        file_context = file_obj.read()
        print(file_context)
except FileNotFoundError:
    print(file_name,'不存在~~~')

三.读取文件

# open()函数默认打开文本文件
with open('demo_01.txt', encoding='utf-8') as file_obj:
    file_context = file_obj.read()
    print(file_context)

# open()函数在读取文件时,会读取整个文件。当文件过大时,会导致内存溢出。open()提供了一个属性size用来规定每次读取的字符个数,size的默认值是-1
# 定义每次读取的长度
with open('demo_01.txt', encoding='utf-8') as file_obj:
    # 定义每次读取的长度
    len = 0
    while len > 0:
        # 每次读取10个字节
        file_context = file_obj.read(10)
        # 输出读取的内容
        print(file_context)
        # 为len重新赋值
        len = len(file_context)
# readline()函数,每次读取一行内容
with open('demo_01.txt', encoding='utf-8') as file_obj:
    while True:
        file_context = file_obj.readline()
        if file_context == '':
            break
        print(file_context, end='')

print('*'*80)

# readlines()函数,一次性的将每行的内容读取出来并存到一个列表中
with open('demo_01.txt', encoding='utf-8') as file_obj:
        file_context = file_obj.readlines()
        print(file_context)
        print(file_context[0])  # Lock horns and battle other players in all the latest .io games. Enjoy original titles like Slither.io and new .

四.写入文件

file_name = 'demo_01.txt'
# 使用open()打开文件时必须要指定打开文件时所要做的操作(读,写,追加)
# 如果不指定操作类型,则默认是‘读取文件’,而此时是不能向文件中写入的
# r 表示只读
# w 表示可写,如果文件不存在会创建文件,如果文件存在则会删除原有内容(截断文件)
# a 表示追加,如果文件不存在会创建文件,如果文件存在则会向文件中追加内容
# r+ 既可读又可写
# w+ 既可写又可读
# a+ 既可追加又可读
with open(file_name, mode='w', encoding='utf-8') as file_obj:
    # write()函数用来向文件中写入内容
    # 如果操作的是一个文本文件的话,则需要传递一个字符串作为参数
    # 该方法可分多次向文件中写入内容
    # 写入完成后,该方法会返回写入的字符的个数
    file_obj.write('锄禾日当午')
posted @ 2022-06-12 18:46  Na氢氧  阅读(56)  评论(0编辑  收藏  举报