|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Generate data entry and reporting .NET Web apps in minutes, straight from your database. Read our FREE whitepaper “Build Web 2.0 Applications Without Hand-Coding” Download now! |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
||||
|
||||
|
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. |
|
#3
|
||||
|
||||
|
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. |
|
#4
|
||||
|
||||
|
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 |
|
#5
|
|||
|
|||
|
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" } } } } |
|
#6
|
||||
|
||||
|
Happy to help.
Your snippet reminds me why I don't like Perl ![]() But of course - each to there own ![]() |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > searching for a specific line in a text file |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|