Python3操作文件系列(二):文件数据读写|二进制数据读写

Python3操作文件系列(二):文件数据读写|二进制数据读写_第1张图片


Python3操作文件系列(一):判断文件|目录是否存在三种方式 

Python3操作文件系列(二):文件数据读写|二进制数据读写

Python3数据文件读取与写入





 

一: 文件数据|二进制数据读写


import os

"""
  Python3的open(file,mode="文件的操作模式")
  利用该函数可以对文件和二进制文件进行只读,只写,读/写和追加等操作
"""
pathFile = '../dataanalysis/file/fileData.txt'
try:
    if os.path.exists(pathFile):
        with open(pathFile, "w") as file:
            file.write("你好,老杨,欢迎来到Python3操作文件的世界!")
            print("数据已成功写入 %s 文件!" % pathFile)
    else:
        print("文件: %s 不存在,下面将为你创建文件...........")
        # 创建文件,mode = w 才会创建不存在的文件
        with open(pathFile, 'w') as f:
            print("文件创建成功!")
except IOError as err:
    print("系统错误: ", err)

print("-----------------一次写入多行数据--------------------")

try:
    with open(pathFile, 'w') as f:
        f.writelines(['Python3 这是第一行数据.....\n', 'Mongodb这是第二行数据....'])
        print("数据已写入")
except IOError as err:
    print("系统异常: ", err)
print()
print("----------------------------读取文件的内容----------------------")

try:
    with open(pathFile, 'r') as file:
        content = file.read()
        print("读到文件内容: %s" % content)
        print()
        print("输出内容后,文件指针已经只写末位,故下面无内容输出")
        print("读起10个字符: ", file.readline(5))
except IOError as err:
    print("系统异常: ", err)

try:
    with open(pathFile, 'r') as file:
        print()
        print("读起10个字符: ", file.readline(6))
except IOError as err:
    print("系统异常: ", err)

try:
    with open(pathFile, 'r') as file:
        print()
        print("读起多行内容(设置只读一行): ", file.readlines(1))
        print("读起多行内容(默认读取所有的行:): ", file.readlines())
except IOError as err:
    print("系统异常: ", err)
binaryPath = "../dataanalysis/file/binaryData.cad"

try:
    with open(pathFile, 'a+') as f:
        f.writelines(['Python3 这是第一行数据.....\n',
                      'Mongodb这是第二行数据....',
                      'Python3 这是第一行数据.....\n',
                      'Python3 这是第一行数据.....\n',
                      'Python3 这是第一行数据.....\n'])
        print("数据已写入")
except IOError as err:
    print("系统异常: ", err)
try:
    with open(pathFile, 'r') as file:
        print()
        for line in file.readlines(-1):
            print("读取文件所有行: ", line)
except IOError as err:
    print("系统异常: ", err)

print('-------------------读写二进制文件数据----------------------')
try:
    with open(binaryPath, 'wb') as file:
        # 用字符串表示坐标数据,转换为字节流,吸入文件
        # 注意数据之间用空格进行分隔
        file.write(bytes(('100000 ' + '10 ' + '20 ' + '29 ' + '22 ' + '30'), 'utf-8'))
except IOError as err:
    print("系统异常: ", err)
print("读取二进制文件")
try:
    with open(binaryPath, 'rb') as file:
        line = file.read().decode("utf-8")
        lines = line.split(" ")
        for item in lines:
            print("存入文件的二进制项为: ", item)

except IOError as err:
    print("系统异常: ", err)

二: 文件数据|二进制数据读写运行效果


D:\program_file_worker\anaconda\python.exe D:\program_file_worker\python_source_work\SSO\grammar\file\FileReadOperationByOpen.py 
数据已成功写入 ../dataanalysis/file/fileData.txt 文件!
-----------------一次写入多行数据--------------------
数据已写入

----------------------------读取文件的内容----------------------
读到文件内容: Python3 这是第一行数据.....
Mongodb这是第二行数据....

输出内容后,文件指针已经只写末位,故下面无内容输出
读起10个字符:  

读起10个字符:  Python

读起多行内容(设置只读一行):  ['Python3 这是第一行数据.....\n']
读起多行内容(默认读取所有的行:):  ['Mongodb这是第二行数据....']
数据已写入

读取文件所有行:  Python3 这是第一行数据.....

读取文件所有行:  Mongodb这是第二行数据....Python3 这是第一行数据.....

读取文件所有行:  Mongodb这是第二行数据....Python3 这是第一行数据.....

读取文件所有行:  Python3 这是第一行数据.....

读取文件所有行:  Python3 这是第一行数据.....

-------------------读写二进制文件数据----------------------
读取二进制文件
存入文件的二进制项为:  100000
存入文件的二进制项为:  10
存入文件的二进制项为:  20
存入文件的二进制项为:  29
存入文件的二进制项为:  22
存入文件的二进制项为:  30

Process finished with exit code 0
 

你可能感兴趣的:(python,numpy,开发语言)