Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 14th, 2005, 2:41 PM   #1
dc6463
Newbie
 
Join Date: Jun 2005
Posts: 6
Rep Power: 0 dc6463 is on a distinguished road
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,
dc6463 is offline   Reply With Quote
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
Old Jun 15th, 2005, 7:26 AM   #3
dc6463
Newbie
 
Join Date: Jun 2005
Posts: 6
Rep Power: 0 dc6463 is on a distinguished road
perfect. works great.

thanks a lot for the help!
dc6463 is offline   Reply With Quote
Old Jun 15th, 2005, 10:30 PM   #4
Dietrich
Professional Programmer
 
Dietrich's Avatar
 
Join Date: Feb 2005
Posts: 434
Rep Power: 4 Dietrich is on a distinguished road
Red face

Quote:
Originally Posted by hydroxide
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.
This gives me an
IOError: [Errno 13] Permission denied: ...
when there are subdirectories present.
__________________
I looked it up on the Intergnats!
Dietrich is offline   Reply With Quote
Old Jun 16th, 2005, 11:55 PM   #5
hydroxide
Programmer
 
Join Date: Apr 2005
Posts: 73
Rep Power: 4 hydroxide is on a distinguished road
Quote:
Originally Posted by Dietrich
This gives me an
IOError: [Errno 13] Permission denied: ...
when there are subdirectories present.
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.
hydroxide is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:10 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC