[Tutor] create a copying tool

Alan Gauld alan.gauld at btinternet.com
Tue May 19 20:33:18 CEST 2015


On 19/05/15 13:20, Remco wrote:

> The source folder looks like this: (with in some cases even sub-folders or sub-sub-folders)
> D:\source\folder1
> D:\source\folder2
> D:\source\folder3
> D:\source\folder4
> D:\source\folder5
>
> And the list.txt looks like this: (over 250 entries in the original list file) D:\source\folder1\1.jpg D:\source\folder1\2.jpg D:\source\folder1\text3.txt D:\source\folder1\FLD\pic.tif
> In every source folder there different kind of extensions that I need to copy.

I'm assu ming the files are on separate lines and
the formatting is an email 'feature'? Please make
sure to post in plain text not HTML.


> I have written the script below, with the help of google, and the only action I see is the missings.txt that is produced.
> With in it all the files that are not copied.

> import os
> import shutil
>
> target_dir = r'D:\target\'
> source_dir = r'D:\source\'
> source_file = r'D:\source\list.txt'
> missing_files = open("missings.txt","w")
>
> for line in open('list.txt'):

Notice that this looks for a list.txt in the same folder
that the script is run from.
You probably wanted to use your source_file variable above:

for line in open(source_file):

> source_file = os.path.normpath(source_dir +line)
> target_file = os.path.normpath(target_dir +line)
> if os.path.exists(target_file):
> shutil.copyfile(source_file,target_file)
> else:
> missing_files.write(line)
> missing_files.close()

Again I assume your indentation got mangled by the
email system Please use plain text for code mails,
its impossible to reliably read your mail otherwise.

I suspect the main issue is that list.txt does not
exist in your current folder or that if it does it
doesn't have the files you expect to find in it.

What does your missing.txt say?
Which files is it failing on?

BTW It looks as if you are maybe closing missing.txt
after the first write so it only ever holds one
file? I suspect the last line should be outside
the loop. But then it may be just the indentation
that is confusing things.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list