[Tutor] copy files selectively from source to destination

Peter Otten __peter__ at web.de
Tue Dec 6 06:13:03 EST 2016


anatta anatta wrote:

> 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?

Such a question almost answers itself when you put a bit more structure into 
your code. You might write a generator that produces (sourcefile, destfile) 
pairs and a copyfile() function that performs the same checks you have 
inlined in your code below. A sketch of the resulting stript:

def filepairs(sourcefolder, destfolder):
    for root, dirs, files in os.walk(sourcefolder):
        for name in files:
             sourcefile = ...
             destfile = ...
             yield sourcefile, destfile

def copyfile(sourcefile, destfile):
    if not os.path.isfile(destfile):
        ... # copy
    else:
        ... # complain
 

for sourcefile, destfile in filepairs("H://", "O://test_o"):
    copyfile(sourcefile, destfile)

To copy only select files you have to add a check to the for loop:

for sourcefile, destfile in filepairs(...):
   if copy_wanted(sourcefile):
       copyfile(sourcefile, destfile)

Now you can experiment with various implementations of that function without 
touching the bulk of your code. To copy only text files it might look like 
this

def copy_wanted(sourcefile):
    return os.path.splitext(sourcefile)[0] == ".txt"

... or this

def predicate_from_glob(glob):
    def is_match(filename):
        return fnmatch.fnmatch(filename, glob)
    return is_match

copy_wanted = predicate_from_glob("*.txt")

... or something completely different like, say, a check based on the MIME 
type.

> 
> Thanks in advance for the hints.
> 
> 
> Best,
> 
> Kumar.
> 
> 
> # -*- coding: utf-8 -*-
> """
> Created on Wed Jun 01 17:05:07 2016
> 
> @author: anatta
> """
> 
> import os
> import shutil
> sourcePath = r'H://'
> destPath = r'O://test_o/'
> ls=os.listdir('.')#list current dir
> #print('listing current dir\n')
> #print(ls)
> 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):
>         os.mkdir(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):
>             try:
>                 shutil.copy2(oldLoc, newLoc)
>                 print 'File ' + f + ' copied.'
>             except IOError:
>                 print 'file "' + f + '" already exists'
> 
> 
> ___
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list