所以...我目前正在学习python。我正在尝试创建一个保存变量值的文件(该文件为arqLog,该变量为novoArq),但是问题是:变量的值以+1更新,但它不会在变量内部发生文件。我希望变量在文件内更新,以便可以将其作为字符串添加到.dat文件名中,并创建某种备份,以便程序继续从停止位置创建.datx文件。(Python 3.4)
x = []
y = []
novoArq = 1
cwd = os.getcwd()
def main():
global novoArq
global cwd
resposta = eval(input('\nChose one of the options below:\n\n1. Create data\n2. Plot data\n3. Quit\n\n--> '))
if resposta == 1:
try:
os.mkdir('coordenadas')
arqLog = open(cwd+'/coordenadas/dat.log','w')
print('\nA new folder has been created: '+cwd+'/coordenadas')
arqLog.write(str(novoArq))
arqLog = open(cwd+'/coordenadas/dat.log','r')
arqLog2 = arqLog.read()
x = input('\nType the values for X separated by coma (ex: -10,2.3,5): ')
y = input('\nType the values for Y separated by coma (ex: -10,2.3,5): ')
arqx = open(cwd+'/coordenadas/x.dat'+arqLog2,'w')
arqx.write(x)
arqx.close()
arqy = open(cwd+'/coordenadas/y.dat'+arqLog2,'w')
arqy.write(y)
arqy.close()
print("\nThese values were saved: "+cwd+"/coordenadas/x.dat"+arqLog2+" e y.dat"+arqLog2)
arqLog.close()
novoArq+=1
main()
except:
arqLog = open(cwd+'/coordenadas/dat.log','r')
arqLog2 = arqLog.read()
x = input('\nType the values for X separated by coma (ex: -10,2.3,5): ')
y = input('\nType the values for Y separated by coma (ex: -10,2.3,5): ')
arqx = open(cwd+'/coordenadas/x.dat'+arqLog2,'w')
arqx.write(x)
arqx.close()
arqy = open(cwd+'/coordenadas/y.dat'+arqLog2,'w')
arqy.write(y)
arqy.close()
print("\nEsses dados foram arquivados em "+cwd+"/coordenadas/x.dat"+arqLog2+" e y.dat"+arqLog2)
arqLog.close()
novoArq+=1
main()
当我打印novoArq时,它输出一个更新的值,但是当我打印arqLog2时,它每次输出1。
你只写novoArq
如果os.mkdir('coordenadas')
没有失败。
第一次失败,所以不要写任何东西1
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句