#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    12
    Rep 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
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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

  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
    programming language development: www.netytan.com Hula

  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    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
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    12
    Rep 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" }
    }
    }
    }
  10. #6
  11. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Happy to help.
    Your snippet reminds me why I don't like Perl

    But of course - each to there own

IMN logo majestic logo threadwatch logo seochat tools logo