Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open file in __init__() python

Tags:

python

file

init

Hey I have a following problem, I need to open a file in __init__(), and with check function I need to check if strings/numbers in rows of this file are the same or not. If they aren't it should return True if they are it should return False, and if there are no more lines None. I do not know how many lines are there going to be in the file. My code is working kind of, tester is giving me 90%, but it says I do not close the file, I understand why it is saying, but do not know where to put the close. However if I opened it with with it should be working but I do not know how to get it working that way.

My Code:

class Program:
    def __init__(self, file_name):
        self.t = open(file_name, 'r')

    def check(self):
        row = self.t.readline()

        array = []

        for i in row.split():
            if i not in array:
                array.append(i)

        if row.split() == []:
            return None
        elif array == row.split():
            return True
        else:
            return False

"""
#testing

if __name__ == '__main__':
    u = Program('file.txt')
    z = True
    while z is not None:
        z = u.check()
        print(z)

"""

Example file:

15 9 22
2014 2015 2014 2015
p py pyt pyth pytho python
ab ab ab ab ab
like image 791
Matis Avatar asked Dec 19 '14 21:12

Matis


People also ask

How do I open a file directly in Python?

Opening Files in PythonPython has a built-in open() function to open a file. This function returns a file object, also called a handle, as it is used to read or modify the file accordingly. We can specify the mode while opening a file. In mode, we specify whether we want to read r , write w or append a to the file.

What is use of __ init __ in Python?

The __init__ method is the Python equivalent of the C++ constructor in an object-oriented approach. The __init__ function is called every time an object is created from a class. The __init__ method lets the class initialize the object's attributes and serves no other purpose. It is only used within classes.

What is __ init __( self in Python?

The self in keyword in Python is used to all the instances in a class. By using the self keyword, one can easily access all the instances defined within a class, including its methods and attributes. init. __init__ is one of the reserved methods in Python. In object oriented programming, it is known as a constructor.

When a file is opened using with open clause in Python?

Opening a File in Python Without Using the With Statement Here is how we can open our file in read mode using the open function. Read mode is the default one. We use the open function to create a file handler (f) and then we use the file handler to read the content of the file using the read() function.


1 Answers

Since you open the file in one method and use it in another, you can't use the with statement internal to the class. You can add a method to close the file and let the closing be the caller's problem. A popular solution for the caller is to use contextlib.closing. Putting it all together...

class Program:
    def __init__(self, file_name):
        self.t = open(file_name, 'r')

    def check(self):
        ...

    def close(self):
        if self.t:
            self.t.close()
            self.t = None


import contextlib
with contextlib.closing(Program('myfile.txt')) as program:
    program.check()
like image 96
tdelaney Avatar answered Sep 21 '22 11:09

tdelaney