Home  >  Article  >  Backend Development  >  Python batch copy files

Python batch copy files

巴扎黑
巴扎黑Original
2016-12-08 09:34:301262browse

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()


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn