file()拷贝文件 分类: python 20121224 16:44 138人阅读 评论(0) 收藏

#! /usr/bin/env python
#coding=utf-8


#拷贝文件,新创建一个空文件,然后读取另外一个文件poem.txt中的内容,写入新建的文件中


#创建一个空文件:file_original.txt

f=file('file_original.txt','a')#追加模式


#从源文件poem.txt中读取文件

f2=file('poem.txt')



n=1


while True:
    line=f2.readline()#读取poem.txt文件中每行的内容
    
    if len(line)!=0:
        #print line
        f.write(line)
        print 'the %d line content:' % n,line,
        n+=1
    else:
        break

f.close()
f2.close()

输入结果:

===============================================

the 1 line content: 
the 2 line content: Programming is fun
the 3 line content: When the work is done
the 4 line content: if you wanna make your work also fun:
the 5 line content:     use Python!
the 6 line content: 
the 7 line content: LovingJune
the 8 line content: .........

===============================================


版权声明:本文为博主原创文章,未经博主允许不得转载。

原文地址:https://www.cnblogs.com/think1988/p/4628261.html