f = open("yesterday", "r")
# 打印前5行
for i in range(5):
print(f.readline())
'''
What's the trick? I wish I knew
爱的诀窍是什么 真希望我能懂
I'm so done with thinking through all the things I could've been
我受够了总是去想我本能完成的所有
And I know you wonder too
'''
# readlines(),每一行最后都有一个换行
print(f.readlines())
# ["What's the trick? I wish I knew \n", '爱的诀窍是什么 真希望我能懂\n']
# 打印文件
for line in f.readlines():
print(line)
'''
What's the trick? I wish I knew
爱的诀窍是什么 真希望我能懂
I'm so done with thinking through all the things I could've been
我受够了总是去想我本能完成的所有
And I know you wonder too
'''
# 剔除换行
for line in f.readlines():
print(line.strip())
'''
What's the trick? I wish I knew
爱的诀窍是什么 真希望我能懂
I'm so done with thinking through all the things I could've been
我受够了总是去想我本能完成的所有
And I know you wonder too
'''
# 打印前五行,low loop
for index, line in enumerate(f.readlines()):
if index < 5:
print(index, line.strip())
'''
0 What's the trick? I wish I knew
1 爱的诀窍是什么 真希望我能懂
2 I'm so done with thinking through all the things I could've been
3 我受够了总是去想我本能完成的所有
4 And I know you wonder too
'''
- f.readlines()只适合读小文件,不适合读大文件
# high bigger mode,一行一行读取,不占内存
for line in f:
print(line.strip())
'''
What's the trick? I wish I knew
爱的诀窍是什么 真希望我能懂
I'm so done with thinking through all the things I could've been
我受够了总是去想我本能完成的所有
And I know you wonder too
'''
# 打印前五行
count = 0
for line in f:
if count < 5:
print(count, line.strip())
count += 1
'''
0 What's the trick? I wish I knew
1 爱的诀窍是什么 真希望我能懂
2 I'm so done with thinking through all the things I could've been
3 我受够了总是去想我本能完成的所有
4 And I know you wonder too
'''
- f.seek()
- f.tell()
# 打印文件指针位置
print(f.tell())
# 0
print(f.readline())
# What's the trick? I wish I knew
print(f.tell())
# 33
f.seek(10)
print(f.readline())
# trick? I wish I knew
# 打印文件名
print(f.name)
# yesterday
# 是否是tty文件
print(f.isatty())
# False
# 文件指针是否能移动
print(f.seekable())
# True
# 文件是否可写
print(f.writable())
# False
# 判断文件是否关闭
print(f.closed)
# False
- f.flush(),将缓存中的数据刷新到硬盘
#!/usr/bin/env python
# coding: utf-8
# Author: Yerban
import sys
import time
# 例如刷新进度条
for i in range(10):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(0.5)
for i in range(10):
sys.stdout.write("#")
time.sleep(0.5)
- f.truncate(),截断,如果没有值,会将文件清空;如果有值,会动文件的开头开始,到指定位置截断。
f.seek(140)
# 就算把文件指针移动,截断也还是从文件头开始。
f.truncate(100)
print(f.read())
'''
we'res the trick? I wish I knew
爱的诀窍是什么 真希望我能懂
I'm so done with thinking
'''
- with语句
为了避免打开文件后忘记关闭,可以通过管理上下文,即:
with open('log','r') as f:
...
如此方式,当with代码块执行完毕时,内部会自动关闭并释放文件资源。
在Python 2.7 后,with又支持同时对多个文件的上下文进行管理,即:
with open('log1') as obj1, open('log2') as obj2:
pass
- 实现简单的shell sed替换功能
with open("yesterday", "r+") as f,\
open("yesterday1", "w+") as f_new:
for i in f:
if "爱的诀窍" in i:
i = i.replace("爱的诀窍", "爱的诀窍BBB")
f_new.write(i)
f_new.seek(0)
print(f_new.read())
通过脚本后面传参数修改:python_name.py 爱的诀窍 爱的诀窍AAA
#!/usr/bin/env python
# coding: utf-8
# Author: Yerban
import sys
with open("yesterday", "r+") as f,\
open("yesterday1", "w+") as f_new:
find_str = sys.argv[1]
replace_str = sys.argv[2]
for i in f:
if find_str in i:
i = i.replace(find_str, replace_str)
f_new.write(i)
f_new.seek(0)
print(f_new.read())
网友评论