您可以将值存储在文件中,然后在启动时加载它们.
代码看起来有点像这样
variable1 = "fi" #start the variable, they can come from the main program instead
variable2 = 2
datatowrite = str(variable1) + "\n" + str(variable2) #converts all the variables to string and packs them together broken apart by a new line
f = file("/file.txt",'w')
f.write(datatowrite) #Writes the packed variable to the file
f.close() #Closes the file !IMPORTANT TO DO!
Run Code Online (Sandbox Code Playgroud)
读取数据的守则是:
import string
f = file("/file.txt",'r') #Opens the file
data = f.read() #reads the file into data
if not len(data) > 4: #Checks if anything is in the file, if not creates the variables (doesn't have to be four)
variable1 = "fi"
variable2 = 2
else:
data = string.split(data,"\n") #Splits up the data in the file with the new line and removes the new line
variable1 = data[0] #the first part of the split
variable2 = int(data[1]) #Converts second part of strip to the type needed
Run Code Online (Sandbox Code Playgroud)
请记住,使用此方法,变量文件与应用程序一起存储在明文中.任何用户都可以编辑变量并更改程序行为