python使用raw_input作为范围文件名

时间:2015-09-02 15:10:24

标签: python file raw-input

目前我有一个脚本,它从原始输入中使用文本文件名(例如1.txt),打开它然后根据我的需要解析它:

fname = raw_input("Enter file name: ")
fh = open(str(fname))
...

目录中有10个文件:1.txt,2.txt,... 10.txt

那么如何在原始输入中输入范围文件名,例如从2.txt到6.txt,并将这些文件1解析为1.而不是每次为单个文件运行脚本?

1 个答案:

答案 0 :(得分:0)

我将假设文件名是n.tx t n >= 1 然后,

def parse(flie):
    pass
    # this is where you parse the file.

lower = raw_input("Enter start: ") # assume int
upper = raw_input("Enter end: ") # assume int and >= lower
files = [str(i)+'.txt' for i in range(int(lower),int(upper)+1)]

for file in files:
    parse(file)