我有一个函数,它接受一个强制文件和其他可能的可选文件。目前,该函数可以从用户输入中读取和写入单个文件。但是我想加载和写入与用户从控制台输入的文件一样多的文件。为此,我希望为一个强制文件名定义函数,并将另一个定义为可选参数。
我如何要求用户在控制台输入中输入一个强制文件和其他所有可能的可选文件名(仅当用户需要时),并在一个函数中分别读取和写入它们,而不会相互混合。我想分别读取和写入所有输入的文件。
基本上,我希望加载用户在控制台输入中输入的所有文件名,并将它们分别写入每个新文件中。
目前,我的函数只从用户输入中加载和读取一个文件。
def read_files(filename, *other):
with open(filename, 'rU') as f:
d = json.load(f)
用户输入:
if __name__ == '__main__':
filename = input('Enter your file name:')
#Here I am confused how do I ask the possible number of filename and How
#do i stop when the user enters all filename
other = input('Enter your other file name')
read_files(filename, )
发布于 2018-06-27 05:08:17
您可以提示quit leads的q
停止添加更多的文件名:
if __name__ == '__main__':
filename = input('Enter your file name:')
other=[]
while True:
inp = input('Enter your other file name (q for quit)')
if inp == 'q':
break
else:
other.append(inp)
read_files(filename, other)
编辑:如果没有输入任何内容,那么停止可能会更方便,因此while循环将是:
while True:
inp = input('Enter your other file name (press ENTER for quit)')
if inp == '':
break
else:
other.append(inp)
https://stackoverflow.com/questions/51051268
复制相似问题