0

I have a python script that import a module that read a file and extract some information from it. This is like this:

import my_module
information = my_module.get_file_info(file_name)
print information.info1()

The module do:

class get_file_info():
    def __init__(self, file_name):
        self.file_name = file_name

    def file_lines(self):
        file_to_get_info = open(self.file_name, 'r')
        lines = file_to_get_info.readlines()
        file_to_get_info.close()
        return lines

    def info1(self):
        info1 = do_something(self.file_lines())
        return info1

The variable 'file_name' is a text file and this is NOT edited by script. When I edit the text file, my class don't see the changes in first time I run the script (like the txt file is not edited), only the second time. What's wrong?

Additional information: I call the main python script from a batch file in windows and I thought it was problem with the pyc files, so I use the "-B" parameter:

python -B main_script.py

I deleted all pyc files and my main python script also import the sys module and has a "sys.dont_write_bytecode = True", but the problem still happens.

André Ginklings
  • 353
  • 1
  • 11
  • 1
    I don' quite understand your problem, but your `get_file_info` class seems not defined / coded properly. See [here](https://learnpythonthehardway.org/book/ex40.html) for a tutorial and the use of `self.` etc. – patrick Apr 01 '17 at 15:32
  • Sorry, I don't put the `self.` here, there is no function "define_something(lines)", just the `self.` in sequence. But do something like: `for line in lines: if 'string' in line: info1 = line self.info1 = info1` – André Ginklings Apr 01 '17 at 16:13
  • I edited the question. – André Ginklings Apr 01 '17 at 16:28
  • Why is your method name and class name same? And why are you taking self.lines as a parameter in your method. Please correct your code. It won't run. – alDiablo Apr 01 '17 at 16:42
  • and where are you passing your parameter "filename"? You want it to go to the class contsructor or to the method itself? Is that an error? – alDiablo Apr 01 '17 at 16:43
  • Indeed, I edited the question with many code errors. Sorry. Now it looks like my real code. The file name is for class constructor. – André Ginklings Apr 01 '17 at 17:36
  • I found my error. My txt file is copied by script and I create the class object before the copy. So, the old file, before the copy, is still there when I run the script. – André Ginklings Apr 01 '17 at 17:57

0 Answers0