提示用户在python中输入文件

时间:2016-06-15 15:03:49

标签: python

我有一个python脚本,它接受两个文件作为参数并运行。有没有办法提示用户输入文件?我见过fileinput方法,但有没有办法单独描述两个文件?

1 个答案:

答案 0 :(得分:2)

我想我现在更了解你了。

import os

file1 = input("please type the directory path for file 1:\n")
file2 = input("please type the directory path for file 2:\n")

def convert_to_log(afloat):
    converted_number = # whatever the conversion equation is. Variable afloat appears here
    return converted_number

def load_probabilities(file_in):
    prob_list = []
    with open(file_in, 'r') as f1:
        for line in f1:
            try:
                prob_list.append(convert_to_log(float(line)))
            except:
                continue
    return prob_list

prob_file1 = load_probabilities(file1)
prob_file2 = load_probabilities(file2)

ratio = list(map(lambda x,y: x/y, a,b)) #calculates probability ratios

print(ratio)

因此我们有一个函数负责加载文件并将概率存储在列表中(load_probabilities),另一个函数在值附加之前将概率转换为对数(convert_to_log)。这有用吗?