The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
searching for a specific line in a text file
Discuss searching for a specific line in a text file in the Python Programming forum on Dev Shed. searching for a specific line in a text file Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 11th, 2004, 05:28 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 12
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
searching for a specific line in a text file
hi,
im trying to rewrite a perl script i have in python. the script just checks that certain directories exist.
i have a configuration file that lists the directories to be checked, like this:
dir:/path/to/wherever:/another/path:/yet/another
however, i can't figure out how to find and read this line only. i have tried to do it in the following way but it doesn't seem to find anything:
confFile = open('/home/me/file.conf', "r").read()
for line in confFile:
if line[0] == "dir:":
print line,
please help, this is driving me mad, as i can't seem to find any relevant docs anywhere
|

February 11th, 2004, 06:35 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
The problem is that file.read() is a string  , so when you loop over this you're actually iterating over each letter. not a line, so you're condition will never be True.
What you actually want is something like this.
Code:
#!/usr/bin/env python
import os
for line in file('paths.txt', 'r'):
if line.startswith('dir:'):
paths = line[4:].split(':')
for path in paths:
if os.path.exists(path): print path,
What this does is iterate over the file instance (line by line) and finds any lines beginning with 'dir:'. Then splits this line slice so we have a bunch of path. Loop over them and check if they exists. If they do then we print the path!
Hope this helps clear things up,
Mark.
__________________
programming language development: www.netytan.com – Hula
|

February 11th, 2004, 06:41 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
While i think about it this would break using absolute paths since C:/whatever/ would be come ['C', '/whatever/']. Maybe you could post you're perl version  i wouldn't mind seeing it if only out of interested .
Mark.
|

February 11th, 2004, 06:45 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Almost there
Code:
confFile = open('/home/me/file.conf', "r").readlines()
#confile is now a list of lines as strings ["line1","line2", ....]
for line in confFile: #try one line at a time
if line.find("dir:") == 0: #using string methods search for 'dir:'. find returns the sub-string position (it returns -1 if not found)
print line,
Grim
|

February 11th, 2004, 07:26 AM
|
|
Registered User
|
|
Join Date: Jan 2004
Posts: 12
Time spent in forums: < 1 sec
Reputation Power: 0
|
|
|
thanks for the help
thanks for the help both of you, i used a mixture of both your replys to get it working.
netytan, although i don't need you to help me with anything more, here is the perl snippet for your curiosity:
if (open CONF, "</home/me/file.conf") {
select CONF;
while (<CONF>) {
chomp;
if (/^dir:/) {
@directories = split /:/, $_;
shift @directories;
foreach (@directories) { print "$_ \n" }
}
}
}
|

February 11th, 2004, 07:35 AM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
Happy to help.
Your snippet reminds me why I don't like Perl
But of course - each to there own 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|