![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jun 2005
Posts: 6
Rep Power: 0
![]() |
how to copy all files containing a certain string in python
how can i copy all files containing a certain string (in the file) to another directory using python on windows?
i got lots of help from the bash forum getting it to work in OS X, but if possible i'd really like to run it straight on the server which is running windows. thanks in advance for your help, |
|
|
|
|
|
#2 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
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. |
|
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jun 2005
Posts: 6
Rep Power: 0
![]() |
perfect. works great.
thanks a lot for the help! |
|
|
|
|
|
#4 | |
|
Professional Programmer
Join Date: Feb 2005
Posts: 434
Rep Power: 4
![]() |
Quote:
IOError: [Errno 13] Permission denied: ... when there are subdirectories present.
__________________
I looked it up on the Intergnats! |
|
|
|
|
|
|
#5 | |
|
Programmer
Join Date: Apr 2005
Posts: 73
Rep Power: 4
![]() |
Quote:
for fname in os.listdir(SOURCEDIR):
sourcefile = os.path.join(SOURCEDIR, fname)
try:
if FINDSTR in open(sourcefile).read():
destfile = os.path.join(DESTDIR, fname)
shutil.copyfile(sourcefile, destfile)
except IOError:
pass--OH. Last edited by hydroxide; Jun 16th, 2005 at 11:58 PM. |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|