-3

I have a simple txt-file which consists of a number of rows. Something like below

Name1
0
0
0
0
Name2
0
0
0
0
Name3
...
...
...

(By ... I mean that the file continues in the same way until infinity hypothetically)

Now my question is, how do I create some kind of a "file control"? I want the file to be used only IF the file outline is of the form presented above.

My current piece of code consists of a Try-Except part as below:

def foo

    try:
        content = open("filename.txt")

    except:
        file1 = [name1,0,0,0,0,name2,0,0,0,0....]
        save(file1)

        content = open("filename.txt")

    lscontent = content.read().splitlines()
    file.close()

What the function save() does is pretty obvious. It only creates and saves the list to a file in the form that I want it to be. Something like initial values. My trouble is that if the user changes the file format or if the file becomes corrupt, then the program will most probably crash.

Any simple ideas which will do this control before launching the program itself? If file is corrupt, I want to replace the file with the initial values, which means I want the file control to be in the try part of the function foo. Ofcourse I want the correct file to have that structure as above.

Diaco
  • 241
  • 1
  • 3
  • 15
  • 1
    1) You never overwrite a file just because it's not in the format you expect. You might overwrite import data because the user typoed a file name. 2) In some form of code would be the only way to check the file format. You could write another program to run before you run this one, but that would be cumbersome and ill-advised. Just throw an error if you encounter a malformed line. 3) Your `try`/`except` is completely broken. The only way into the `except` block is for the creation of `file` to error out, so `file` doesn't exist inside the `except` block. Also, use `with` for file operations. – jpmc26 Jan 11 '15 at 21:43
  • My way of thinking is this: I want to create a list of objects where every row in the file is an object. If there doesn't exist a file with that name and with the specific outline that I want, then I want to create the file with some initial values. How would you implement that? – Diaco Jan 11 '15 at 21:51
  • http://stackoverflow.com/q/2967194/1394393. Then you just write as normal. – jpmc26 Jan 11 '15 at 21:52
  • I cant see how my try/except is broken. I havent included the save function in this piece of code and that could be why it isnt working for you. Because I am currently running the program as I want, but I just want to create the "file control" – Diaco Jan 11 '15 at 21:53
  • the logic of the code is that if the file "filename.txt" does not exist, then file cannot be created so it will jump into except and create the file using that piece of code. It is working, so not sure why my logic wouldnt be correct – Diaco Jan 11 '15 at 21:57
  • Oh, I've misunderstood. You're re-using `file` for different data. In that case, your code following the try/except is broken if it makes it into the `except` block, since `file` won't hold a file-like object if it goes through `except`. Use different variable names for different things; it makes your code more readable and less error prone. Also, `file` is a built in; I highly recommend against using it as a variable name since it will shadow the built in. Also, `open` can throw errors for more reasons than just the file not existing. You could be denied read permissions, for instance. – jpmc26 Jan 11 '15 at 21:58
  • Sorry, look at it now. It should be working! In my actual program I'm using another variable name. Sorry for being a bit sloppy here with some of the names.. I do realize that I am probably doing it in a complicated way, but I couldnt come up with anything simpler. This is actually my first formal program so bear with me. – Diaco Jan 11 '15 at 22:01

1 Answers1

0
from shutil import copyfile
import re

is_number_or_varname = re.compile("(\d+|[a-z]\w*)$", re.I).match

def get_good_file(fname, line_rule, default_fname):
    with open(fname) as inf:
        if all(line_rule(line) for line in inf):
            inf.seek(0)
            return inf.readlines()
    # else
    copyfile(default_fname, fname)
    with open(default_fname) as inf:
        return inf.readlines()

data = get_good_file("filename.txt", is_number_or_varname, "defaults.txt")
Hugh Bothwell
  • 55,315
  • 8
  • 84
  • 99