2016-06-15 23 views
-1

我有一個Python腳本,它以兩個文件作爲參數並運行。有什麼方法可以提示用戶輸入文件嗎?我看過fileinput方法,但是有什麼辦法可以單獨描述兩個文件嗎?提示用戶輸入Python中的文件

+0

這個問題很不清楚。預期投入和預期產出是多少? – freakish

+0

你想讓你的python腳本實際加載文件嗎? – LAL

+0

我需要一些類似於raw_input加載文件,並將比代碼將去: – oezlem

回答

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)值獲得附加之前其他功能。這會工作嗎?

+0

不要忘記使用os.path.abspath(路徑)返回的內容。 –

+0

該代碼不會加載文件。它類似於raw_input() – LAL

+0

@LAL現在就可以了。 –