python file

//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 ')
fo.write('456 ')
fo.write('789 ')
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 ", "am ", "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 ', '456 ', '789']
next 123

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

原文地址:https://www.cnblogs.com/woodzcl/p/7803192.html