Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old February 11th, 2004, 05:28 AM
mattcronin57 mattcronin57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 12 mattcronin57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #2  
Old February 11th, 2004, 06:35 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #3  
Old February 11th, 2004, 06:41 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #4  
Old February 11th, 2004, 06:45 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
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

Reply With Quote
  #5  
Old February 11th, 2004, 07:26 AM
mattcronin57 mattcronin57 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2004
Posts: 12 mattcronin57 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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" }
}
}
}

Reply With Quote
  #6  
Old February 11th, 2004, 07:35 AM
Grim Archon's Avatar
Grim Archon Grim Archon is offline
Mini me.
Dev Shed Novice (500 - 999 posts)
 
Join Date: Nov 2003
Location: Cambridge, UK
Posts: 783 Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)Grim Archon User rank is Corporal (100 - 500 Reputation Level)  Folding Points: 1488 Folding Title: Novice Folder
Time spent in forums: 3 Days 2 h 15 m 57 sec
Reputation Power: 7
Send a message via MSN to Grim Archon
Happy to help.
Your snippet reminds me why I don't like Perl

But of course - each to there own

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > searching for a specific line in a text file


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway