day03_01 文件操作
Posted flytoyou
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了day03_01 文件操作相关的知识,希望对你有一定的参考价值。
with 语句
__author__ = "Alex Li"
import sys
#f = open("yesterday2","r",encoding="utf-8")
with open("yesterday2","r",encoding="utf-8") as f ,\\
open("yesterday2", "r", encoding="utf-8") as f2:
for line in f:
print(line)
打印进度条
# Author:Adminone
import sys,time
for i in range(20):
sys.stdout.write("#")
sys.stdout.flush() #刷新缓存文件,加快磁盘存取文件
time.sleep(0.1)
yesterday[1-3]
Oh, yesterday when I was young
噢 昨日当我年少轻狂
So many, many songs were waiting to be sung
有那么那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
There are so many songs in me that won\'t be sung
我有太多歌曲永远不会被唱起
I feel the bitter taste of tears upon my tongue
我尝到了舌尖泪水的苦涩滋味
The time has come for me to pay for yesterday
终于到了付出代价的时间 为了昨日
When I was young
当我年少轻狂
file_op.py
# Author:Adminone
"""
data = open("yesterday",encoding="utf-8").read()
print(data)
"""
\'\'\'
#f = open("yesterday2",\'w\',encoding="utf-8") #w 创建文件,清空文件数据(不建议使用),不能读
f = open("yesterday3",\'a\',encoding="utf-8") #文件句柄,创建文件,不能读
#a = append 追加
f.write("\\nwhen i was young i listen to the radio\\n")
data = f.read()
print(\'--read\',data)
f.close()
\'\'\'
#high bige
f = open("yesterday2",\'r\',encoding="utf-8")
count = 0
for line in f:
if count == 9:
print(\'----我是分割线----------\')
count += 1
continue
print(line)
count +=1
\'\'\'
#low loop
f = open("yesterday2",\'r\',encoding="utf-8")
for index,line in enumerate(f.readlines()): #enumerate() 取每行的下标
if index == 9:
print(\'----我是分割线----------\')
continue
print(line.strip()) #清除换行符
\'\'\'
#for i in range(5):
# print(f.readline())
\'\'\'
f = open("yesterday2",\'r\',encoding="utf-8")
print(f.tell())
#print(f.readline(10))
print(f.readline())
print(f.readline())
print(f.readline())
print(f.tell()) #记录读取字符数
f.seek(0) #回到文件首行
print(f.readline())
f.seek(10)
print(f.readline())
\'\'\'
\'\'\'
f = open("yesterday2",\'r\',encoding="utf-8")
print(f.encoding)
print(f.fileno()) #返回内存编号
print(f.seekable()) #判断文件是否可移到首行
print(f.readable()) #True False
print(f.writable())
\'\'\'
\'\'\'
f = open("yesterday3",\'a\',encoding="utf-8")
print(f.flush()) #刷新缓存文件,加快磁盘存取文件
print(dir(f.buffer) )
#print(f.close()) #判断文件是否关闭
#f.truncate() #文件清空
f.seek(10)
f.truncate(20) #截断
\'\'\'
\'\'\'
f = open("yesterday2",\'r+\',encoding="utf-8") #文件句柄 读写 在后面追加
print(f.readline())
print(f.readline())
print(f.readline())
f.write("-----------diao-----------")
\'\'\'
\'\'\'
#f = open("yesterday2",\'w+\',encoding="utf-8") #文件句柄 写读 创建清空后写入
#f = open("yesterday2",\'a+\',encoding="utf-8") #文件句柄 追加读写
#没什么卵用
f.write("\\n-----------diao-----------\\n")
f.write("-----------diao-----------\\n")
f.write("-----------diao-----------\\n")
f.write("-----------diao-----------\\n")
print(f.tell())
f.seek(10)
print(f.tell())
print(f.readline())
f.write("should be at thr begining of the second line")
f.close()
\'\'\'
\'\'\'
f = open("yesterday2",\'rb\') #文件句柄 二进制文件 读
print(f.readline())
print(f.readline())
print(f.readline())
print(f.readline())
\'\'\'
\'\'\'
f = open("yesterday2",\'wb\') #文件句柄 二进制文件 写
f.write("hello binary".encode("utf-8")) #字符集转换 二进制转字符串
f.close()
\'\'\'
f = open("yesterday2",\'ab\') #文件句柄 二进制文件 追加
f.write("hello binary\\n".encode("utf-8")) #字符集转换 二进制转字符串
f.close()
文件修改
# Author:Adminone
import sys
f = open("yesterday2","r",encoding="utf-8")
f_new = open("yesterday2.bak","w",encoding="utf-8")
# find_str = sys.argv[1]
# replace_str = sys.argv[2]
find_str = "肆意的快乐等我享受"
replace_str = "肆意的快乐等Alex享受"
for line in f:
if find_str in line:
line = line.replace(find_str,replace_str)
f_new.write(line)
f.close()
f_new.close()
自动关闭打开的文件
# Author:Adminone
import sys
#f = open("yesterday2","r",encoding="utf-8")
with open("yesterday2","r",encoding="utf-8") as f ,\\
open("yesterday2", "r", encoding="utf-8") as f2:
for line in f:
print(line)
以上是关于day03_01 文件操作的主要内容,如果未能解决你的问题,请参考以下文章