python调用cmd复制文件代码分享

yipeiwu_com6年前Python基础


复制代码 代码如下:

import os

def load() :
    filename = os.getcwd() + r'\fromto.txt'
    if os.path.isfile(filename) :       
        f = open(filename)
        try :
            lines = f.readlines()
        finally :
            f.close()
            return lines
    else :
        print('请创建fromto.txt.')
        input()
        exit()

def display(_lines) :
    linenum = 1
    s = '序号 源文件 目标文件\n'
    for line in _lines :
        s += str(linenum) + ' ' + line
        linenum += 1
    return s + '\n' + r'请输入序号:'

def work(s, _lines) :
    cmd = r'copy /y ' + _lines[int(s)-1]
    print(cmd)
    os.system(cmd)

if __name__ == "__main__" :

    lines = load()

    while True :
        try :
            s = input(display(lines)).strip()
            if s.lower() == 'exit' :
                break
            if int(s) == 0 :
                lines = load()
                print('已重新加载')
                continue

            work(s, lines)

        except :
            input('--Error--')



fromto.txt的格式是每一行左边是源文件,右边是目标文件(或目标目录)。
反正其实就是包装了copy一下。

fromto.txt内容

复制代码 代码如下:

d:\txt\1.txt d:\txt\2.txt
d:\txt\*.txt d:\txt2\

相关文章

Python实现基于TCP UDP协议的IPv4 IPv6模式客户端和服务端功能示例

本文实例讲述了Python实现基于TCP UDP协议的IPv4 IPv6模式客户端和服务端功能。分享给大家供大家参考,具体如下: 由于目前工作的需要,需要在IPv4和IPv6两种网络模式...

使用Pandas的Series方法绘制图像教程

使用Pandas的Series方法绘制图像教程

通常绘制二维曲线的时候可以使用matplotlib,不过如果电脑上安装了pandas的话可以直接使用Series的绘图方法进行图像的绘制。 pandas绘制图像其实也是给予matplot...

使用Python的Django框架结合jQuery实现AJAX购物车页面

使用Python的Django框架结合jQuery实现AJAX购物车页面

Django中集成jquery 首先,静态的资源通常放入static文件夹中: static/ css/ djquery.css samples/...

Jupyter notebook在mac:linux上的配置和远程访问的方法

upyter Notebook已经逐渐取代IDE成为了多平台上写简单Python脚本或应用的几家选择。 Jupyter Notebook可以通过pip/pip3安装: pip3 inst...

django模型中的字段和model名显示为中文小技巧分享

简单方法: models.py 复制代码 代码如下: class IceCreamBar(models.Model):     title =  ...