19.python-文件操作

file_name = 'data_file_pointer.txt'


def print_txt(file_name):
    try:
        with open(file_name) as f:
            for line in f.readlines():
                print(line)
    except FileNotFoundError as nf:
        print(nf)

def clear_txt(file_name):
    try:
        with open(file_name,'r+') as f:
            f.truncate()
    except FileNotFoundError as nf:
        print(nf)


# open(file, mode='r', buffering=-1, encoding
# =None,errors=None, newline=None, closefd=True, opener=None)
# 打开一个文件并返回文件对象,如果打开失败,抛出OSError异常
# file是文件路径(支持绝对路径和相对路径)

# mode 控制文件的操作权限
# 'r':读文件(默认值)
# 'w':清空文件并开启写模式
# 'x': 创建文件并打开,如果文件已存在则失败
# 'a':在文件末尾追加文件
# 'b':字节模式
# 't':字符模式(默认值)
# '+':打开文件读写操作

# 可读模式(r)   执行步骤:
#   1.文件是否存在?是:打开文件可读模式,否:抛出FileNotFoundError
try:
    with open(file_name) as f:
        print('r mode')
except FileNotFoundError as nf:
    print(nf)
# [Errno 2] No such file or directory: 'test.txt'

# 可写模式 (w)  执行步骤:
#   1.文件是否存在?是:打开文件可写模式,否:创建文件并打开文件可写模式;
#   2.清空文件
print('w:')
for i in range(3):
    with open(file_name, 'w') as f:
        f.write(str(i))
print_txt(file_name)
# w:
# 2

clear_txt(file_name)
# 追加模式(a)   执行步骤:
#   1.文件是否存在?是:打开文件追加模式,否:创建文件并打开文件追加模式;
# 和可写模式的区别:可写模式-每次打开文件的同时会清空文件,而追加模式不会
print('a:')
for i in range(3):
    with open(file_name, 'a') as f:
        f.write(str(i))
print_txt(file_name)
# a:
# 012

print('字符模式:')
try:
    with open(file_name) as f:
        for c in f.read():
            print(c, end=' ')
except FileNotFoundError as nf:
    print(nf)
# 字符模式:
# 0 1 2

print()
print('字节模式:')
try:
    with open('data1.txt', 'rb') as f:
        for b in f.read():
            print(b, end=' ')
except FileNotFoundError as nf:
    print(nf)
print()
# output:
# 字节模式:
# 48 49 50

# 对于同一次打开文件,文件对象有且仅有一个指向当前操作位置的指针,例如:
# 对文件刚刚执行完追加操作之后,立即读取文件内容,会什么都读不到,因为追加之后文件指针指向文件尾部

print('file pointer:')

# w+:先按照可写模式打开文件,同时开启文件可读权限,执行步骤:
#   1.打开文件,并开启可写可读模式 2.清空文件 3.进行读写操作
# r+:先按照可读模式打开文件,同时开启文件可写权限,执行步骤:
#   1.打开文件,并开启可写可读模式 2.文件开头进行读写操作,如果文本不空,则初始内容会被覆盖
# a+:先按照追加模式打开文件,同时开启文件可读权限,执行步骤:
#   1.打开文件,并开启追加可读模式 2.在文件尾部进行读写操作(对于个别UNIX系统,不一定从尾部执行操作)
# w+先清空再从文件开头读写,r+直接从文件开头读写,a+直接从文件末尾读写
# f.seek(n)在w+和r+模式下在第n+1处开始读写,如果文本长度小于n则通过空格补齐


try:
    with open(file_name, 'w+') as f:
        f.writelines(['qqqqqqqqqqq'])
except FileNotFoundError as nf:
    print(nf)
print('1:')
print_txt(file_name)
# 1:
# qqqqqqqqqqq

try:
    with open(file_name, 'w+') as f:
        f.writelines(['hello python'])
except FileNotFoundError as nf:
    print(nf)
print('2:')
print_txt(file_name)
# 2:
# hello python


try:
    with open(file_name, 'r+') as f:
        f.writelines(['123'])
except FileNotFoundError as nf:
    print(nf)
print('3:')
print_txt(file_name)
# 3:
# 123lo python

try:
    with open(file_name, 'r+') as f:
        f.seek(20)
        f.writelines(['123'])
except FileNotFoundError as nf:
    print(nf)
print('4:')
print_txt(file_name)
# 4:
# 123lo python        123

try:
    with open(file_name, 'w+') as f:
        f.seek(20)
        f.writelines(['hello word'])
except FileNotFoundError as nf:
    print(nf)
print('5:')
print_txt(file_name)
# 5:
#                     hello word

try:
    with open(file_name, 'a+') as f:
        f.seek(1)  # 在a或者a+模式下该步无效
        f.writelines(['hello word'])
except FileNotFoundError as nf:
    print(nf)
print('6:')
print_txt(file_name)
# 6:
#                     hello wordhello word


# buffering
#   -1(default):按系统默认缓存策略
#   0:不使用缓冲(由于字符操作必须要使用缓存,故字符模式下,buffering不能为0)
#   1:缓冲一行(在字节模式下不支持)
#   大于1:指定缓冲区大小


# Python文件操作不依赖操作系统
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值