. 文件操作 .
""""
测试目标
1。访问模式对文件的影响
访问模式对write()的影响
3访问模式是否可以省略
"""
# r :文件不存在,报错
# f = open("test.txt", "r")
# a = True
# while a:
# a = f.read(7)
# print(a)
# f.close()
# read() 空白字符全部输出 readlines() readline()
"""
尝试目标
1。r指针位置开头 + w+ a末尾+区别
2。文件指针对数据读取的印象
"""
# seek(偏移量,起始位置)
# f = open("test.txt", "rb")
# print(f.readline())
# f.seek(2, 1)
# print(f.readline())
# f.seek(2, 1)
# print(f.readline())
# 文件备份
old_name = input("输入备份文件名:")
print(old_name)
index = old_name.find(".")
new_name = old_name[:index] + ["备份"] +old_name[index:]
old_f = open(old_name, "rb")
new_f = open(new_name, "wb")
while True:
a = old_f.read()
if len(a) == 0:
break
new_f.write(a)
old_f.close()
new_f.close()
|
|