[Tutor] copy files selectively from source to destination
kumarmysore at hotmail.com
Mon Dec 5 11:20:56 EST 2016
Here is my working code - to copy files from one drive to another.
I however want to copy selective files.
For example I like to copy only .txt files only from the source to destination, and not other types of files.
How could I do this selective copying?
Thanks in advance for the hints.
# -*- coding: utf-8 -*-
Created on Wed Jun 01 17:05:07 2016
sourcePath = r'H://'
destPath = r'O://test_o/'
ls=os.listdir('.')#list current dir
#print('listing current dir\n')
for root, dirs, files in os.walk(sourcePath):
#figure out where we're going
dest = destPath + root.replace(sourcePath, '')
#if we're in a directory that doesn't exist in the destination folder
#then create a new folder
if not os.path.isdir(dest):
print 'Directory created at: ' + dest
#loop through all files in the directory
for f in files:
#compute current (old) & new file locations
oldLoc = root + '\\' + f
newLoc = dest + '\\' + f
if not os.path.isfile(newLoc):
print 'File ' + f + ' copied.'
print 'file "' + f + '" already exists'
More information about the Tutor