Python更新并在执行期间保存

时间:2014-01-01 15:36:27

标签: python self-updating

我不确定如何用这句话来标题。

Python中是否有一个方法可以从终端获取输入并将该输入写入脚本并保存该更改?换句话说,脚本将是自我更新的。用户在提示符处输入一些字符序列,脚本会将这些字符写入脚本正文。下次执行脚本时,这些字符将可供参考。

2 个答案:

答案 0 :(得分:3)

你可以重写源文件,是的。它只是一个文件,读取和写入文件在Python中是完全可行的。 Python只需一次加载源文件,因此重写它们肯定是可能的。

但是,使用单独的文件编写序列会更容易,并在下次运行代码时从该文件中读取它。

Python有any number of data persistence modules,可以帮助您更轻松地在文件中读取和编写序列。或者,您可以重复使用JSON等数据序列化标准来处理读写问题。这些将更容易维护。

答案 1 :(得分:0)

以下脚本以第2行和第3行开头,不包含文件名 -

#! /usr/bin/env python3
#
#
import os.path
no_file_1, no_file_2 = False, False
#open the file containing this script and read, by line, into list 'a', close this file
thisfile = open('__file__','r')
a = []
while True:
    file_line = thisfile.readline()
    if not file_line:
        break
    else:
        a.append(file_line)
thisfile.close()
#extract the file names from line 2 and 3 of this file (one file name per line)
file_1, file_2 = a [1], a [2]
file_1, file_2 = file_1[1:-1], file_2[1:-1]
#if there are no file(s) listed in line 2 and 3, prompt the user for file name(s)
#and write the file name(s) w/ extenstion '.txt' to a[1] and a[2]
if file_1 == '':
    no_file_1 = True
if file_2 == '':
    no_file_2 = True
if no_file_1:
    file_1 = input('Enter 1st File Name (no extension) >>> ')
    a [1] = '#' + file_1 + '.txt' + '\n'
if no_file_2:
    file_2 = input('Enter 2nd File Name (no extension) >>> ')
    a [2] = '#' + file_2 + '.txt' + '\n'
#... then write a[new script] to this script
if no_file_1 or no_file_2:
    thisfile = open(__file__, 'w')
    for i in a:
        thisfile.write(i)
#now that this script contains file names in lines 2 and 3, check to see if they exist
#in the same directory as this script ... if not, create them.
file_1, file_2 = a [1], a [2]
file_1, file_2 = file_1[1:-1], file_2[1:-1]
if not os.path.exists(file_1):
    open(file_1, 'w')
    #write to file_1.txt
if not os.path.exists(file_2):
    open(file_2, 'w')
thisfile.close()
print(file_1,file_2)

执行脚本,检查并在第2行和第3行中找不到文件名。用户输入两个文件名,脚本用正确的文件名覆盖自身,检查两个文件是否存在,如果不存在,脚本创建它们。

Enter 1st File Name (no extension) >>> file_1
Enter 2nd File Name (no extension) >>> file_2

下次执行脚本时,文件名在第2行和第3行中定义,脚本会检查它们是否存在,如果不存在,脚本会创建它们。

#! /usr/bin/env python3
#file_1.txt
#file_2.txt
import os.path
no_file_1, no_file_2 = False, False
#open the file containing this script and read, by line, into list 'a', close this file
thisfile = open(__file__,'r')
a = []
while True:
    file_line = thisfile.readline()
    if not file_line:
        break
    else:
        a.append(file_line)
thisfile.close()
#extract the file names from line 2 and 3 of this file (one file name per line)
file_1, file_2 = a [1], a [2]
file_1, file_2 = file_1[1:-1], file_2[1:-1]
#if there are no file(s) listed in line 2 and 3, prompt the user for file name(s)
#and write the file name(s) w/ extenstion '.txt' to a[1] and a[2]
if file_1 == '':
    no_file_1 = True
if file_2 == '':
    no_file_2 = True
if no_file_1:
    file_1 = input('Enter 1st File Name (no extension) >>> ')
    a [1] = '#' + file_1 + '.txt' + '\n'
if no_file_2:
    file_2 = input('Enter 2nd File Name (no extension) >>> ')
    a [2] = '#' + file_2 + '.txt' + '\n'
#... then write a[new script] to this script
if no_file_1 or no_file_2:
    thisfile = open(__file__, 'w')
    for i in a:
        thisfile.write(i)
#now that this script contains file names in lines 2 and 3, check to see if they exist
#in the same directory as this script ... if not, create them.
file_1, file_2 = a [1], a [2]
file_1, file_2 = file_1[1:-1], file_2[1:-1]
if not os.path.exists(file_1):
    open(file_1, 'w')
    #write to file_1.txt
if not os.path.exists(file_2):
    open(file_2, 'w')
thisfile.close()
print(file_1,file_2)