Python文件复制(txt文件)

功能:这个py脚本是把一个txt文件(源文件)复制到另一个txt文件(目的文件)里面
算法思路:
程序首先判断源文件(用exists函数判断)和目的文件是否存在,如果不存在则输出文件路径不存在,如果存在则先把源文件读出来,再写到目的文件中去

#coding=utf-8
from sys import argv

from os.path import exists 

script,from_name,to_name=argv   #提前输入2个文件路径
if exists(from_name):  #如果源文件存在路径存在继续执行程序
    if exists(to_name):  #如果目的文件路径存在继续执行程序

        source=open(from_name)  #打开源文件

        indate=source.read()  #显示所有源文件内容

        print "Your source is from %s and it is %s bytes"%(from_name,len(from_name)) #告诉用户要复制的源文件内容的大小 
        print "Now start copyying ..."

        target=open(to_name,'w')  #打开目的文件

        target.write(indate)

        print "Copy sucessfully and display!"

        source.close()

        target.close()

        target2=open(to_name)

        print target2.read()

        target2.close()
    else: #目的文件路径不存在
        print "your %s don\'t exist"%to_name
else: #源文件路径不存在
    print "your %s don\'t exist"%from_name
posted @ 2017-09-25 19:56  不变1024  阅读(2764)  评论(0编辑  收藏  举报