1

This is actually a very basic issue i'm facing

# create list using append & idiom method to test process time
fln=open('CROSSWD.TXT')

def check_1(fln):
    res=[]
    for line in fln:
        word=line.strip()
        res.append(word) # just create a new list
    return res

def check_2(fln):
    res2=[]
    for line in fln:
        word2=line.strip()
        res2+=[word2] # using another way
    return res2

n=check_2(fln) # now this where the problem occurs. n returns the value
m=check_1(fln) # m return a void list

# it should call both m,n & print same length. They work separately but calling at once does'nt work why?
print (len(n))
print(len(m))

But if I run them separately they work as intended. This is a very basic issue, hope somone can clarify me on this basics

Chidhvilas
  • 85
  • 8
  • 3
    Perhaps, you may need to open/close the file in both functions. Once the file is read, there isnt anything more to read. So, the first function called will read the content and reach end of file. The 2nd function will have nothing to read then. – shahkalpesh Jun 27 '20 at 20:14

3 Answers3

0

The file is "used up" by reading it in check_2. The call of check_1 is trying to continue stepping through the same file, but the end of that file has been reached by the end of the call to check_2.

To read it twice, call fln=open('CROSSWD.TXT') twice.

Another point: Your code neglects to close the file. In a script which exits right after reading a file, you can leave it to the operating system to close the file on exit. But still, you should get use to opening files with the context manager pattern, using with and indenting the block that uses the file.

with fln=open('CROSSWD.TXT'):
    res=[]
    for line in fln:
        word=line.strip()
        res.append(word) # just create a new list
    return res
Joshua Fox
  • 18,704
  • 23
  • 87
  • 147
0

the problem is that you read the file in your first function call till the end thus there is nothing left. Moreover, you are never closing the file. Thats why it is recommended to use a context manager to interact with files like this:

fln='CROSSWD.TXT'

def check_1(fln):
    res=[]
    with open(fln) as file:
        ctx = file.read()

    for line in ctx:
        word=line.strip()
        res.append(word) # just create a new list
    return res

def check_2(fln):
    res2=[]

    with open(fln) as file:
        ctx = file.read()

    for line in fln:
        word2=line.strip()
        res2+=[word2] # using another way
    return res2

if __name__ == "__main__":
    n=check_2(fln) 
    m=check_1(fln) 

    print(len(n))
    print(len(m))
  • Please do not recommend to ``.read()`` a file only to iterate over it. This loads the entire file into memory, which can be problematic for large files. Instead, iterate over the file itself in the ``with`` statement. – MisterMiyagi Jun 27 '20 at 20:54
-1

Open the file twice.

def check_1(fln):
    res=[]
    for line in fln:
        word=line.strip()
        res.append(word) # just create a new list
    return res


def check_2(fln):
    res2=[]
    for line in fln:
        word2=line.strip()
        res2+=[word2] # using another way
    return res2


n=check_2(open('CROSSWD.TXT', 'r'))  # now this where the problem occurs. n returns the value
m=check_1(open('CROSSWD.TXT', 'r')) # m return a void list

# it should call both m,n & print same length. They work separately but calling at once does'nt work why?
print(len(n))
print(len(m))