Python trying to write and read class from file but something went horribly wrong
<p>Considering this is only for my homework I don't expect much help but I just can't figure this out and honestly I can't get my head around what's going wrong. Usually I have an idea where the problem is but now I just don't get it.</p>

<p><em>Long story short</em>: I'm trying to create a valid looking telephone number within a class and then loading it onto an array or list then later on save all of them as string into a folder. When I start the program again I want it to read the file and re-create my class and load it back into the list. (Basically a very simple repository).</p>

<p>Problem is even though I evaluate the stored phone number in the exact same way I validate it as input data ... I get an error which makes no sens.</p>

<p>Another small problem is the fact that when I re-use the data for some reason it creates white spaces in the file which in turn messes my program up badly.</p>

<p>Here I validate phone numbers:</p>

<pre><code>def validateTel(call_ID):
    if isinstance (call_ID, str) == True:
        call_ID = call_ID.replace (" ", "")
        if (len (call_ID) != 10):
            print ("Telephone numbers are 10 digits long")
            return False
        for item in call_ID:
            try:
                int(item)
            except:
                print ("Telephone numbers should contain non-negative digits")
                return False
            else:
                if (int(item) &lt; 0):
                print ("Digits are non-negative")
</code></pre>

<p>After this I use it and other non-relevant (to this discussion) data to create an object (class instance) and move them to a list.</p>

<p>Inside my class I have a load from string and a load to string. What they do is take everything from my class object so I can write it to a file using <code>"+"</code> as a separator so I can use <code>string.split("+")</code> and write it to a file. This works nicely, but when I read it ... well it's not working.</p>

<pre><code>    def load_data():
        f = open ("data.txt", "r")
        ch = f.read()
        contact = agenda.contact () # class object
        if ch in (""," ","None"," None"):
           f.close()
           return []  # if the file is empty or has None in some way I pass an empty stack
        else:
           stack = [] # the list where I load all my class objects
        f.seek(0,0)
        for line in f:
            contact.loadFromString(line) # explained bellow
            stack.append(deepcopy(contact))
        f.close()
        return stack
</code></pre>

<p>In <code>loadFromString(line)</code> all I do is validate the line (see if the data inside it at least looks OK).</p>

<p>Now here is the place where I validate the string I just read from the file:</p>

<pre><code>def validateString (load_string):
    string = string.split("+")
    if len (string) != 4:
        print ("System error in loading from file: Program skipping segment of corrupt data")
        return False
    if string[0] == "" or string[0] == " " or string[0] == None or string[0] == "None" or string[0] == " None":
        print ("System error in loading from file: Name field cannot be empty")
    try:
        int(string[1])
    except:
        print("System error in loading from file: ID is not integer")
        return False
    if (validateTel(str(string[2])) == False):
        print ("System error in loading from file: Call ID (telephone number)")
        return False
    return True
</code></pre>

<p><strong>Small recap</strong>:
I try to load the data from file using <code>loadFromString()</code>. The only relevant thing that does is it tries to validate my data with <code>validateString(string)</code> in there the only thing that messes me up is the validateTel. But my input data gets validated in the same way my stored data does. They are perfectly identical but it gives a "System error" BUT to give such an error it should have also gave an error in the validate sub-program but it doesn't.</p>

<p>I hope this is enough info because my program is kinda big (for me any way) however the bug should be here somewhere. </p>

<p>I thank anyone brave enough to sift trough this mess.</p>

<p>EDIT:</p>

<p>The class is very simple, it looks like this:</p>

<pre><code>    class contact:
         def __init__ (self, name = None, ID = None, tel = None, address = None):
             self.__name = name
             self.__id = ID
             self.__tel = tel
             self.__address = address
</code></pre>

<p>After this I have a series of setters and getters (to modify contacts and to return parts of the abstract data)</p>

<p>Here I also have my loadFromString and loadToString but those work just fine (except maybe they cause a small jump after each line (an empty line) which then kills my program, but that I can deal with)</p>

<p>My problem is somewhere in the validate or a way the repository interacts with it. The point is that even if it gives an error in the loading of the data, first the validate should print an error ... but it doesn't -_-</p>
