Discussion:
[Tutor] create a copying tool
Remco
2015-05-19 12:20:03 UTC
Permalink
Hello,

I am new here, and new with Python.
I use Python 2.7 on a windows computer in Pyscripter.

I've got the next problem witch I need some help with:

I need to copy several files, from several folders to a new location in the same folder structure as they are now.
And all the files I need to copy are in a text (.txt) file.

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 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'):
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()
Can someone help my fixing my problem.

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-05-19 18:33:18 UTC
Permalink
Post by Remco
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.
Post by Remco
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")
Notice that this looks for a list.txt in the same folder
that the script is run from.
Post by Remco
source_file = os.path.normpath(source_dir +line)
target_file = os.path.normpath(target_dir +line)
shutil.copyfile(source_file,target_file)
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


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Jos Kerc
2015-05-19 18:52:13 UTC
Permalink
Hi,
Post by Remco
Hello,
<snip>
Post by Remco
import os
import shutil
target_dir = r'D:\target\'
source_dir = r'D:\source\'
source_file = r'D:\source\list.txt'
you are not using this...
Post by Remco
missing_files = open("missings.txt","w")
source_file = os.path.normpath(source_dir +line)
target_file = os.path.normpath(target_dir +line)
don't you want to check if the source file is existing?
Post by Remco
shutil.copyfile(source_file,target_file)
missing_files.write(line)
missing_files.close()
Can someone help my fixing my problem.
_______________________________________________
https://mail.python.org/mailman/listinfo/tutor
HTH,
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...