返回 Python实...... 登陆

Python实现拷贝多个文件到同一目录的方法

巴扎黑 2017-01-09 17:43:28 433

这篇文章主要介绍了Python实现拷贝多个文件到同一目录的方法,涉及Python针对文件与目录的遍历、复制等相关操作技巧,需要的朋友可以参考下

本文实例讲述了Python实现拷贝多个文件到同一目录的方法。分享给大家供大家参考,具体如下:

有一个文件,里面存有多个文件名,一个文件名一行。如果想把这些文件拷贝到一个目录,可以用下面的代码。下面的代码应该是跨系统的,除了分隔文件全路径那一句。下面的代码假设拷贝所有文件到当前目录的tmp子目录下,目录需先创建.

#encoding=utf-8
import sys
import shutil
if len(sys.argv) < 2:
  print u'缺少参数文件名'
  exit(-1)
par_file = sys.argv[1]
file_lst = []
try:
  with open(par_file, 'r') as file:
    for line in file:
      file_lst.append(line)
except IOError as err:
  print('File error: ' + str(err))
  exit(-1)
idx = 1
for item in file_lst:
  print idx
   item.strip()
   lst = src.split('\\') #这句针对windows
  dst = './tmp/' + lst[-1]
  print '==============================================================================='
  print src
  print '-------------------------------------------------------------------------------'
  print dst
  print '==============================================================================='
  shutil.copyfile(src, dst)
  idx += 1

第19行的“src = ”被编辑器干掉了。

最新手记推荐

• 用composer安装thinkphp框架的步骤 • 省市区接口说明 • 用thinkphp,后台新增栏目 • 管理员添加编辑删除 • 管理员添加编辑删除

全部回复(0)我要回复

暂无评论~
  • 取消 回复 发送
  • PHP中文网