0

I am using something similar to the question here:

Python: Undo a Python file readline() operation so file pointer is back in original state

but seems to be misbehaving.

My code: (values at runtime shown as comments)

def parse_shell(self, filename):
    output = None
    with open(self.shells_path + filename) as fp:
        first_line = fp.readline()
# first_line = 'Last login: Tue Jun  9 07:13:26 on ttys0'
        while len(string.split(first_line, '$')) == 1:
            last_pos = fp.tell()
            # last_pos = 43
            first_line = fp.readline()
# first_line =' 2015-06-09 08:18:23 [coryj@Corys-MacBook-Pro ~]$ ping 10.2.2.1'

        fp.seek(last_pos)
        for line in fp.readline():
            # line = ''
            line_chunks = string.split(line, '$')

according to the docs, readline() should only return an empty string if it hits EOF. This particular file is over 200k so I don't see why I would get an empty string.

Community
  • 1
  • 1
user916499
  • 85
  • 1
  • 1
  • 7
  • `for line in fp.readline()` is totally bogus – o11c Jun 26 '15 at 23:54
  • as @o11c said, `for line in fp.readline()` will give you the characters in the line. so the first character is whitespace. try changing it to `fore lin in fp:` – Lawrence Benson Jun 26 '15 at 23:56
  • that does indeed appear to be the problem. If one or the other of you gentlemen would write it as an answer, I'll be more than happy to accept it. – user916499 Jun 26 '15 at 23:59
  • @user916499 You can answer your own questions btw. [you even get a badge for it](https://stackoverflow.com/help/badges/14/self-learner) if you get enough upvotes – o11c Jun 27 '15 at 00:20
  • unrelated: `first_line` is a misleading name. You could use `lines_with_dollar = (line for line in file if '$' in line)` instead. – jfs Jun 27 '15 at 03:10

3 Answers3

0

The problem is that the statement:
while len(string.split(first_line, '$')) == 1: can not be True, so the script never will enter the while loop. string.split() will result in list of minimum two member, like ["some",""], so len = 2 at least.

Furthermore while loop will run while the statement True, and quits if it turn in to False.

I hope it helped! Feel free to accept my answer if you feel it was useful to you. :-)

Geeocode
  • 5,705
  • 3
  • 20
  • 34
  • string. split will returm one element if the $ is not found. I do this specifically to skip all lines from the top to the first one with a $ in it – user916499 Jun 27 '15 at 03:17
0

like o11c and lawrence said:

for line in fp.readlines() is valid

for line in fp is valid

for line in fp.readline() is not a valid construct.

user916499
  • 85
  • 1
  • 1
  • 7
0

I don't get what is string.split? Do you mean str.split? From what you are trying to do - it looks like - you want to split all lines that have $ in them into parts and ignore other lines. And in your particular case those lines seem to be at the beginning of file. A simpler approach would be

for line in fp:
    if line.find('$') >= 0:
        line.split('$')  # or str.split(line, '$')

Others have pointed out this already, still - line in fp.readlines() is different from line in fp.readline(). Former would iterate over all lines of a file collected in a list, latter will iterate over all characters of only one line.

What you are trying to do is line in fp.readline() is actually trying to iterate over the contents of the line - which is probably not what you want.

' 2015-06-09 08:18:23 [coryj@Corys-MacBook-Pro ~]$ ping 10.2.2.1'

So change it to line in fp (preferred) or line in fp.readlines(). and you should be good

Hope that helps.

gabhijit
  • 3,345
  • 2
  • 23
  • 36
  • This meant to parse a shell session log of indeterminate length, figure out which lines are commands, when they were run, and what the output of those commands was. Then throw that all in a database so it can be searched by time. I went with `line in fp` for now. I still have a billion edge cases to deal with, but I'm making progress. – user916499 Jun 28 '15 at 15:54
  • Use `if '$' in line:` instead of `if line.find('$') >= 0:` – jfs Jun 28 '15 at 17:46