Copying files from sub folders under source directories into sub folders with same names as source directory sub folders in destination directories without overwriting already existing files of same name.

Satish ML satishmlwizpro at gmail.com
Tue May 20 08:24:47 EDT 2014


On Tuesday, May 20, 2014 5:51:19 PM UTC+5:30, Satish ML wrote:
> On Tuesday, May 20, 2014 11:27:01 AM UTC+5:30, Rustom Mody wrote: > On Monday, May 19, 2014 2:32:36 PM UTC+5:30, Satish ML wrote: > On Monday, May 19, 2014 12:31:05 PM UTC+5:30, Chris Angelico wrote: > > On Mon, May 19, 2014 at 4:53 PM, wrote: > Could you kindly help? Sure. Either start writing code and then post when you have problems, or investigate some shell commands (xcopy in Windows, cp in Linux, maybe scp) that can probably do the whole job. Or pay someone to do the job for you. ChrisA > Hi ChrisAngelico, > Consider that source and destination directories are given in a .xls(excel) file. > This is the code > import xlrd, sys, subprocess > file_location = "C:\Users\salingeg\Desktop\input.xls" > workbook = xlrd.open_workbook(file_location) > sheet = workbook.sheet_by_index(0) > sheet.cell_value(0, 0) > for row in range(sheet.nrows): > values = [] > values.append(sheet.cell_value(row, 1)) > destination = [] > destination.append(sheet.cell_value(row, 2)) > for s in values: > for d in destination: > If I am using cp or xcopy command, it will copy all files from s to d. > shutil.copy(s, d) can't be used here because it overwrites files in d. Kindly help. have u tried using https://docs.python.org/2/library/os.path.html#os.path.exists ? I have tried it. But how does it help? We won't be able to make out whether source file is present in destination directory.

If we can do that, like 

if (source file exists in destination directory) 
 print "exists" 
 continue
else 
 shutil.copy(s, d) 



More information about the Python-list mailing list