Visit Jeremy's Blog.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 05-08-2008, 01:33 PM   #1
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Rep: Reputation: 43
Python File#readline skips to end of file

Hello all, I'm having a problem reading line-at-a-time from a file in Python. Basically, I've got some code like this:
    while not
        print ("# %06d > " % self.handle.tell()) + line[:80].rstrip()
        line = self.handle.readline()
Where self.commentStartRegex = re.compile(r'^\s*/\*\*'), and which is parsing some input code:
  final ArrayList<Method> methods = Something.getInstance().lookupSomething(someClass, function);
  if(null == methods || methods.size() == 0)
    throw new NotImplementedException(someClass.toString() + " does not implement the function: " + function);
  return methods;

 * This is a comment.
When I run it I get something more or less like this:
# 004367 >   final ArrayList<Method> methods = Something.getInstance().lookupSomething(someCl
# 004412 >   if(null == methods || methods.size() == 0)
# 004521 >     throw new NotImplementedException(someClass.toString() + " does not implemen
# 004539 >   return methods;
# 004542 > }
# 004543 > 
# 013261 > 
# 013261 > 
... [keeps going ad infinitum]
So it seems that this code is somehow jumping to the end of the file. I don't understand why that is. Plus, there's nothing odd in the file, just printing characters, tabs, and Unix newlines. Does anybody have any ideas why this could be happening?
Old 05-08-2008, 04:54 PM   #2
Registered: Mar 2007
Distribution: OpenBSD-CURRENT
Posts: 485
Blog Entries: 160

Rep: Reputation: 74
Is this part of a bigger, perhaps threaded application?

time.sleep(#) gives up the GIL and could potentially allow a reference to the open file to be read outside of this code...though it seems it'd be exceptionally intermittent if that was the case.

You got me stumped on this one...I've tried threading and all...can't replicate the problem.

Is this code inside a bigger block of code that loops?
Old 05-09-2008, 06:25 AM   #3
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Original Poster
Rep: Reputation: 43
No, it's not threaded, which is why I find this really odd. I threw the sleep in there so I could read the output before it suddenly started flying by, so it doesn't change the outcome. And it happens repeatedly, namely every single time the program runs. It's not file-dependent, either, it happens in other source code that contains the same kind of scenario. I'm going to investigate it closer this morning, try to recreate it with as small a chunk of code as possible, but right now it very much seems like that regex matches, then the pointer in the file gets set to the end, then the condition in the loop somehow fails and it keeps repeating. I don't understand it, hopefully some playing around in ipython will yield some clues.
Old 05-09-2008, 09:32 AM   #4
Registered: Jun 2005
Location: Indiana, USA
Distribution: OpenBSD, Ubuntu
Posts: 892

Original Poster
Rep: Reputation: 43
OK, now I feel stupid. It was another regex later on that I *thought* I had shown to be correct which was causing (using a similar modus operandi) the file to be read until the end. <sigh> I guess select() wasn't broken after all.



Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Python: how to import a python file that doesn't end in .py? BrianK Programming 5 04-15-2008 12:35 PM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM
how to delete last number/word of a file and incude file count at the end of the chennaiguy Linux - Newbie 2 02-18-2008 09:08 PM
SUSE 10.1 - Install skips to the end after first CD contillion SUSE / openSUSE 6 11-07-2006 06:27 AM
Script, Reading a file, When end of file? elibm Programming 2 07-16-2001 11:01 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:37 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration