Help please with code to find and move files.

inFocus at sl.com inFocus at sl.com
Sun Dec 30 21:04:23 EST 2007


Hello,

I am new to python and wanted to write something for myself where
after inputing two words it would search entire drive and when finding
both names in files name would either copy or move thoe files to a
specified directory.

But couple of attempts did not work as desired this is one of them.
Could someone help fix it or maybe give a better example.

 Thank you very much.


import os, os.path, shutil

path = r"c:\\"
dest_file = 'C:\\files'
name_search = raw_input('Please enter name searchs : ').split()
dup = []


for root, dirs, files in os.walk(path):
    for name in files:
		file_name = os.path.join(root, name)
		if (name_search[0] in file_name) and (name_search[1]
in file_name):
			#if os.path.join(root, name) in dest_file:
			if file_name in dup:
				break
			else:
				print "copied %s to %s" % (name,
dest_file)
				shutil.copy(os.path.join(root, name),
dest_file)
				dup.append(file_name)



More information about the Python-list mailing list