2012-07-12 148 views
-6

我很難將2個函數嵌套到更大的函數中,因此函數將打開並讀取文件並打印結果。沒有錯誤,但結果顯示爲None。請讓我知道我可以如何改進代碼。先謝謝你!嵌套函數-python

import glob 

#Read a result file and return a list 
def read_results(filename): 
    text_file=open(filename,"r") 
    lines=text_file.readlines() 
    return lines 
    text_file.close() 

#Give a list of results files in your directory  
def get_filename(): 
    filelist=glob.glob("./Data/*.*") 
    return filelist 

#Call above functions to get file names and read each file 
def read_lines(filename): 
    def get_filename() 
     def read_results(): 
      print lines 

#main 
function=read_results("./Data/GSM21203-GSM21215.csv") 
print"\nHere's the lines of the text file:", function 

#In order to use the strip method, it must be a str. Currently I am not using a   string.  Figure out how to do it 

print"\n" 

list_of_filenames=get_filename() 
print"Here are the list of filenames:",list_of_filenames 

read_each_file=read_lines("*.*") 
print "Here are the contents of each file",read_each_file 

回答

4

你應該完全重新思考你是如何做到這一點的。你的代碼似乎表明你不明白函數如何在Python中工作。例如:

def read_lines(filename): 
    def get_filename() 
     def read_results(): 
      print lines 

。 。 。創建三個功能,但不會調用其中的任何一個。

首先閱讀the Python tutorial

+0

謝謝你的迴應,我是一名初學者學習Python,並且我仍然試圖理解函數。你有什麼建議嗎? – user1521735 2012-07-12 20:10:53

+0

是的,我建議你閱讀我在答案中鏈接的Python教程。 – BrenBarn 2012-07-13 03:35:42