1

我不知道如何表达这个问题的标题。

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

4

2 回答 2

3

你可以重写源文件,是的。它只是一个文件,在 Python 中读写文件是完全可行的。Python 只加载一次源文件,因此重写它们当然是可能的。

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

Python 具有任意数量的数据持久性模块,可以帮助更轻松地读取和写入文件中的序列。或者您可以重用JSON等数据序列化标准来为您处理读取和写入。这些将更容易维护。

于 2014-01-01T15:37:43.463 回答
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)
于 2014-01-02T20:40:22.047 回答