您的位置:首页 > 编程语言 > Python开发

Python文件操作2

2016-08-29 18:31 113 查看
文件操作:

r 读

r+ 读写

w 写入 重新创建

w+ 读写入 重新创建

a 写入 追加 创建

a+ 读写 追加 创建

b 以二进制打开文件

U 支持所有换行符

f = file('hello.txt',"a+")
f.writelines('chejia\n')
f.writelines('123456\n')
f.flush()
f.seek(0)
print f.encoding
print f.mode
print f.tell()
print  f.readline()
f.seek(0)
print f.readlines()
f.close()
#文件的赋值
src = file('hello.txt','r')
dst = file('hello word.txt','w')
dst.write(src.read())
dst.close()
src.close()

#文件的删除
import os.path
if os.path.exists('hello.txt'):
os.remove('hello.txt')
print 'del'

输出:
a+
0
chejia

['chejia\n', '123456\n']
del
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: