码迷,mamicode.com
首页 > 编程语言 > 详细

python file

时间:2017-11-08 13:19:53      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:str   truncate   getc   false   python   als   ace   sed   mod   

//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‘]

python file

标签:str   truncate   getc   false   python   als   ace   sed   mod   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!