0

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?

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Such a hack and makes me cringe so badly. Still, it's such an elegant hack, that it deserves a +1
I see from your edited question that you're using py2exe. This solution won't work for that case as it stands. Setting up an executable file so that it is successfully self-modifying is possible but much harder than doing so with a simple Python script.
2

Seems like what you want to do would better be solved using the Windows Registry - I am assuming that since you mentioned you'll be creating an exe from your script.

This following snippet tries to read a string from the registry and if it doesn't find it (such as when the program is started for the first time) it will create this string. No files, no mess... except that there will be a registry entry lying around. If you remove the software from the computer, you should also remove the key from the registry. Also be sure to change the MyCompany and MyProgram and My String designators to something more meaningful.

See the Python _winreg API for details.

import _winreg as wr

key_location = r'Software\MyCompany\MyProgram'
try:
    key = wr.OpenKey(wr.HKEY_CURRENT_USER, key_location, 0, wr.KEY_ALL_ACCESS)
    value = wr.QueryValueEx(key, 'My String')
    print('Found value:', value)
except:
    print('Creating value.')
    key = wr.CreateKey(wr.HKEY_CURRENT_USER, key_location)
    wr.SetValueEx(key, 'My String', 0, wr.REG_SZ, 'This is what I want to save!')
wr.CloseKey(key)

Note that the _winreg module is called winreg in Python 3.

1 Comment

+1: A good option if you know that the script will be running on Windows, as appears to be the case for this question.
0

Why don't you just put it at the beginning of the code. E.g. start your code:

import ... #import statements should always go first

path = 'what you want to save'

And now you have path saved as a string

4 Comments

the import statement has nothing to do with it. All I'm saying is that if you do import some library into your code, you should start with that
this wouldn't be persistent, though. path would be reset to same value each time the script is run.
As I understood that was what you wanted? If not, hopefully someone else can answer
No, I need a way to save text in the program, but without using an extra file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.