简体   繁体   English

在没有文本文件的情况下用Python保存数据?

[英]Saving data in Python without a text file?

I have a python program that just needs to save one line of text (a path to a specific folder on the computer). 我有一个python程序,只需要保存一行文本(计算机上特定文件夹的路径)。

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. 但是,我更喜欢仅使用python文件的解决方案。

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? 因此,我问:即使关闭了python程序,也没有任何方法可以在不创建任何新文件的情况下将文本保存在python程序中?

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? 编辑:之后我正在使用py2exe使程序成为.exe文件:也许该文件可以存储在其中,所以就好像没有文本文件一样?

You can save the file name in the Python script and modify it in the script itself, if you like. 您可以将文件名保存在Python脚本中,然后根据需要在脚本本身中对其进行修改。 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. 该脚本本身读取到一个列表,然后再次打开它本身写作和修改该行savefile设置。 Each time the script is run, the change to the value of savefile will be persistent. 每次运行脚本时,对savefile值的更改将保持不变。

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. 我不一定会推荐这种自我修改的代码作为一种好的做法,但是我认为这可能是您想要的。

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. 使用Windows注册表可以更好地解决您想做的事情-我假设既然您提到过,您将通过脚本创建一个exe。

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. 另外,请确保将MyCompanyMyProgram以及My String指示符更改为更有意义的内容。

See the Python _winreg API for details. 有关详细信息,请参见Python _winreg API

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. 请注意, _winreg模块在Python 3中称为winreg

Why don't you just put it at the beginning of the code. 为什么不把它放在代码的开头。 Eg 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 现在,您已将路径另存为字符串

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM