Saving data in Python without a text file?
Question: I have a python program that just needs to save one line of text (a path to a
specific folder on the computer).

I've got it working to store it in a text file and read from it; however, I'd
much prefer a solution where the python file is the only one.

And so, I ask: is there any way to save text in a python program even after
its closed, without any new files being created?

**EDIT:** I'm using py2exe to make the program an .exe file afterwards: maybe
the file could be stored in there, and so it's as though there is no text
file?


Answer: You can save the file name in the Python script and modify it in the script
itself, if you like. For example:

    
    
    import re,sys
    
    savefile = "widget.txt"
    x = input("Save file name?:")
    lines = list(open(sys.argv[0]))
    out = open(sys.argv[0],"w")
    for line in lines:
        if re.match("^savefile",line):
            line = 'savefile = "' + x + '"\n'
        out.write(line)
    

This script reads itself into a list then opens itself again for writing and
amends the line in which `savefile` is set. Each time the script is run, the
change to the value of `savefile` will be persistent.

I wouldn't necessarily recommend this sort of self-modifying code as good
practice, but I think this may be what you're looking for.

