newbie: windows xp scripting

Larry Bates larry.bates at websafe.com
Mon May 22 09:47:04 EDT 2006


oscartheduck wrote:
> For completeness' sake, this is the new script I came up with:
> 
> import os
> 
> dirfrom = 'C:\\test'
> dirto = 'C:\\test1\\'
> makedir = 'mkdir "%s"' % dirto
> copy_command = 'copy "%s" "%s"' % (dirfrom, dirto)
> 
> if os.system(copy_command) == 0:
>     print "yay"
> else:
>     if os.system(makedir) == 0:
>         if os.system(copy_command) == 0:
>             print 'yay!'
>         else:
>             print 'WTF mate?'
> 
> 
> Is there a more efficient way of doing this?
> 
Suggested changes:

import os
import glob
import shutil

dirfrom = 'C:\\test'
dirto = 'C:\\test1'

if not os.path.exists(dirto): os.makedirs(dirto)
for src in glob.glob(os.path.join(dirfrom, "*.*")):
    dst=os.path.join(dirto, os.path.basename(src))
    #print "Copying %s->%s" % (src, dst)
    shutil.copyfile(src, dst)


-Larry Bates



More information about the Python-list mailing list