代码改变世界

python file

2017-11-08 11:30  woodzcl  阅读(168)  评论(0编辑  收藏  举报

//test.py

fo = open('f.txt', 'wb')
print 'name',fo.name
print 'closed',fo.closed
print 'mode',fo.mode
print 'softspace',fo.softspace
fo.write('123\n')
fo.write('456\n')
fo.write('789\n')
fo.write('000')
fo.close()
fo = open('f.txt', 'rb+')
str = fo.read(3)
print str
print 'tell',fo.tell()
print 'seek',fo.seek(0, 0)
print 'readline',fo.readline()
print 'tail line'
fo.seek(-4, 2)
fo.truncate()
fo.seek(0,0)
print 'readlines',fo.readlines()
fo.seek(0,0)
print 'next',fo.next()
fo.seek(8,0)
str = fo.read(3)
print str
seq = ["I\n", "am\n", "fool"]
fo.writelines(seq)
fo.seek(0,0)
print 'readlines',fo.readlines()
fo.close()

import os

os.rename('f.txt','ff.txt')
os.remove('ff.txt')

os.mkdir('test')
os.mkdir('test/1')
cur = os.getcwd()
os.chdir('./test/1')
os.getcwd()
os.chdir(cur)
os.rmdir('test/1')
os.rmdir('test')

//result

# python test.py
name f.txt
closed False
mode wb
softspace 0
123
tell 3
seek None
readline 123

tail line
readlines ['123\n', '456\n', '789']
next 123

789
readlines ['123\n', '456\n', '789I\n', 'am\n', 'fool']