View Single Post
Old Jun 14th, 2005, 11:26 PM   #2
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
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.
hydroxide is offline   Reply With Quote