I am new to Python; little experience in programming C++. I saw this question but it doesn't address my problem.
Python 2.7.9, 64-bit AMD, Windows 7 Ultimate, NTFS, administrator privileges & no "read only" attribute on file to be read.
I want to create a list of strings which fulfill a certain criteria, the strings are lines of the file(see notepad.cc/diniko93).So I wrote the following function-
def makeLineList( filePtr, ptr ):
    lines = []
    while True:
        s = filePtr.readline()
        if not s=="":
            s = s[3:]
            s = s.split()
            if s[0].isdigit():
                print("O")
                lines.append(s)
            elif s[0] in {"+", "-"}:
                print("U")
                lines.append(s)
        else:
            print("none")
            break
    filePtr.seek(ptr, 0);    #I did this to restore file pointer, so other functions accessing this file later don't misbehave
    return lines
and the 2 possible main()-like (pardon my ignorance of python) bodies that I am using are-
with open("./testStage1.txt", 'r') as osrc:
    osrc.seek(291, 0)
    L = makeLineList( osrc, osrc.tell())
    print "".join(L)
and the other one-
osrc = open("./testStage1.txt", 'r')
osrc.seek(291, 0)
L = makeLineList( osrc, osrc.tell())
print "".join(L)
osrc.close()
both the times the output on terminal is a disappointing none
Please Note that the code above is minimum required to reproduce the problem and not the entire code.
EDIT:
Based on @avenet's suggestion, I googled & tried to use iter (__next__ obj.next() in python 3.3+ or next(obj) in 2.7) in my code but the problem persists, I am unable to read next line even if I call next(osrc) from inside the function check out these 2 snippets
- version2 next used only in main()-ish part transform_line function is not called. Calling next() 3 times produces desirable/expected output but in
- version3 I get a list index out of range error, even for lists[0] which definately has a digit
EDIT 2: I tried scope check inside my functions as if not osrc in locals(): and in next line with proper indent print("osrc not reachable"). And the output is osrc not reachable. I also tried using from tLib import transform_line from a temporary tLib.py but with identical results. Why is osrc not available in either case?
EDIT 3: Since the problem appears to be of scope. So to avoid passing of file variable- make a function whose sole purpose is to read a line. The decision to get next line or not depends upon returned value of a function like isLineUseful()
def isLineUseful( text, lookFor ):
    if text.find(lookFor)!=-1:
        return 1
    else:
        return 0
def makeList( pos, lookFor ):
    lines = []
    with open("./testStage1.txt", 'r') as src:
        src.seek(pos)
        print(src.read(1))
        while True:
            line = next(src)
            again = isLineUseful(line, lookFor)
            if again==0:
                src.seek(pos)
                break
            else:
                lines.append(line)
    return lines
t = makeList(84, "+")
print "\n".join(t)
Tried it, it works perfectly on this(notepad.cc/diniko93) sample testStage1.txt.
So my programming issue is solved (thanks to responders :D) & I am marking this as answered but posting a new question about the anomalous/ behavior of readline() & __next__.
P.S. I am still learning the ways of python so I would be very happy if you could suggest a more pythonic & idomatic version of my code above.
 
     
     
     
     
    
". "<" is not a digit, and it's not "-" or "+", so it makes sense that none of those conditions in your code would succeed. You're using `strip`, right? Are you sure you're using it right? Remember, just calling strip does nothing if you don't assign the result. `s.strip("
")` has no effect, you have to do `s = s.strip("
")`.
– Kevin Dec 30 '14 at 14:30