|
#coding=utf-8
#
# 文件 操作
#
# w; r; rb; b; a(追加模式); w+; r+
file = open(r'E:\py_code\1234.txt', 'w+')
#print file
file.write('123')
file.close()
file = open(r'E:\py_code\1234.txt', 'r+')
#print file
print file.read(10)
file.close()
#管道
import sys
#text = sys.stdin.read()
#words = text.split()
#wordcount = len(words)
#print 'wordcount', wordcount
##Linux 命令
#cat 操作文件 | python 当前代码文件 | sort
# 上面代码统计单词
#基本用法
file = open(r'E:\py_code\test.txt')
print file.read(10) # 字节读取
print file.read()
file.close()
#readline 用法; 行读取
file = open(r'E:\py_code\test.txt')
for i in range(3):
print str(i) + ':' + file.readline()
file.close()
#writelines 用法
file = open(r'E:\py_code\test.txt', 'w')
file.writelines('i like your face') # 会清空,写入
file.close()
#文件对象迭代
def process(string):
print 'c:', string
def show(filename):
file = open(filename)
if True:
#while True:
#char = file.read(1)
#if not char:break
#process(char)
for line in file.readlines():
process(line)
else:
#char = file.read(1)
#while char:
#process(char)
#char = file.read(1)
for char in file.read():
process(char)
file.close
show(r'E:\py_code\test.txt')
|
|
|