用python实现的可以拷贝或剪切一个文件列表中的所有文件

yipeiwu_com5年前Python基础
复制代码 代码如下:

# coding:utf-8
import os
import sys
def cut_and_paste_file(source, destination):
    '''
    source: file path 中文
    destination: directory path
    '''
    def format_path(path):
        if not os.path.isabs(path):
            path = os.path.join(os.getcwd(), path)
        return path
    def mk_dir(path):
        if not os.path.exists(os.path.dirname(path)):
            mkdir_cmd = 'mkdir "%s"' % os.path.dirname(path)
            print os.popen(mkdir_cmd).read()

    destination = os.path.join(format_path(destination), source)
    source = format_path(source)
    mk_dir(source)
    mk_dir(destination)
    copy_cmd = 'copy /Y "%s" "%s"' % (source, destination)
    print 'copy_cmd:%s' % copy_cmd
    print os.popen(copy_cmd).read()
    del_cmd = 'del "%s" /Q' % source
    print 'del_cmd:%s' % del_cmd
    print os.popen(del_cmd).read()
if __name__ == '__main__':
    if len(sys.argv) != 2:
        print 'params must be 1,the params is the file of contain the list of cutAndPastFile List'
        exit(0)

    file_name = sys.argv[1]
    f = open(file_name, 'r')
    lst_file = f.readlines()
    f.close()

    output_path = 'backup_del'

    for filename in lst_file:
        filename = filename.replace('\n', '')
        if filename != '':
            cut_and_paste_file(filename, output_path) 


传一个文件给该py文件即可,例如,文件名为:del_file.txt
group1_input\subgroup13\55657_XSL_Transformations_(XSLT)_Version_2.0.doc
group1_input\subgroup6\377-6700-001 REV B .doc
group3_input\subgroup42\CGP_Manual_5_0.doc

相关文章

Python绘制正余弦函数图像的方法

Python绘制正余弦函数图像的方法

今天打算通过绘制正弦和余弦函数,从默认的设置开始,一步一步地调整改进,让它变得好看,变成我们初高中学习过的图象那样。通过这个过程来学习如何进行对图表的一些元素的进行调整。 01. 简单绘...

python使用正则表达式的search()函数实现指定位置搜索功能

前面学习过search()可以从任意一个文本里搜索匹配的字符串,也就是说可以从任何位置里搜索到匹配的字符串。但是现实世界很复杂多变的,比如限定你只能从第100个字符的位置开始匹配,100...

python+selenium实现简历自动刷新的示例代码

python+selenium实现简历自动刷新的示例代码

本文用到的文件的下载地址 百度网盘链接: https://pan.baidu.com/s/1tmpdEfAZKff5TOMAitUXqQ 提取码: e6at 1 安装Python 和 s...

python的id()函数介绍

>>> a = 2.5>>> b = 2.5>>> c = b>>> a is cFalse>>>...

实例讲解Python中的私有属性

在Python中可以通过在属性变量名前加上双下划线定义属性为私有属性,如例子: 复制代码 代码如下: #! encoding=UTF-8   class A:  &n...