# coding=utf-8 # !/usr/bin/python # -*- coding: UTF-8 -*- import io import os def file_chance(): #修改文件内某一行的内容 f = open('text1', 'r') w = open('text2', 'w') n = 0 for i in f: n += 1 if n == 3: i = "yes,i am\n" w.write(i) f.close() w.close() os.remove('text1') os.rename('text2', 'text1') def file_del(): #删除文件内容 f = open('text1','w') f.close() def file_write(): #文件写入20行you are a winner f = open('text1', 'w') for i in range(20): f.write('you are a winner\n') f.close() file_del() file_write() file_chance() ''' 总结: "i=' '.join(['123456'])" = "i+‘123456’" f.tell() 指针的位置 f.read(*) 读取*个字符,中文占3个位置,英文占一共位置 f.seek(*) 改变指针的位置 r+模式 tell在最末尾的位置 w+模式 tell在第一位置且对象内容为空 a+模式 tell在末尾位置 写/删:w/w+/a/a+ 改:读取一个文件,写入另一个文件,将读取文件删除,将写入文件重新命名为读取文件 '''