Quote:
|
Originally Posted by dc6463
how can i copy all files containing a certain string (in the file) to another directory using python on windows?
|
Assuming that none of your files are going to be huge (ie: hundreds of megabytes) the following should work:
import os
import shutil
SOURCEDIR = "C:/Somedir"
DESTDIR = "C:/Anotherdir"
FINDSTR = "Somestring"
for fname in os.listdir(SOURCEDIR):
sourcefile = os.path.join(SOURCEDIR, fname)
if FINDSTR in open(sourcefile).read():
destfile = os.path.join(DESTDIR, fname)
shutil.copyfile(sourcefile, destfile)
--OH.