Home > Article > Backend Development > Python batch copy files
An excel is exported from the database, which contains the file names of some image files. The corresponding files need to be downloaded from the server. The program does not provide a batch export function for images. It is just temporary data statistics. You need to manually export the corresponding files in excel. .
1. Copy the file name column in excel, paste it into a blank text file, name it filelist.txt, and upload it to the server.
2. Use script export on the server, python script:
#! python #coding:utf-8 ##!/usr/bin/python # Filename : fileCp.py import sys import os import shutil fileList='filelist.txt' targetDir='files' filedir = open(fileList) line = filedir.readline() log = open('running.log','w') while line: line = line.strip('\n'); basename = os.path.basename(line) exists = os.path.exists(line) if exists : print 'copy '+line+' to '+os.getcwd()+'/'+targetDir+'/'+basename log.write('copy '+line+' to '+os.getcwd()+'/'+targetDir+'/'+basename+'\r\n') shutil.copy(line,targetDir+'/'+basename) else: print line+' not exists' log.write(line+' not exists'+'\r\n') line = filedir.readline() log.close()