[Tutor] Simple copy script

Alan Gauld alan.gauld at btinternet.com
Mon Apr 14 09:42:23 CEST 2008


"Que Prime" <queprime at gmail.com> wrote

> This is what I came up with after writing it out and reading the
> corresponding functions.  I feel I'm close but something is still 
> awry.

You need to give us more information.
What is still awry? Do you get any errors or is it not
copyng all files? Or any of them?

Here are some possibilities:

Do all the files you are copying exist? - Are you 100% sure?
Does the 'outfile' folder exist? Does it exist inside C:\test?

To try and fix things:

Add some print lines to print:
1) The current folder( use os.getcwd() )
2) Each filename
3) A directory listing of outfile after copying the files

Finally a couple of comments:

> #file copy based on input file containing filenames to be copied
>
> ##############################
> import os
> import shutil
>
> os.chdir('c:\\test')
> infile = open("input.txt","r")
>
> for line in infile:

You can miss out the open() line by putting it into the for loop:

for line in open('input.txt','r'):

>  shutil.copy(line, 'outfolder')

You may want to strip() the line first just in case any extra
whitespace has been added in the file


>  #print line 'copied'
> infile.close()

HTH

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list