-7

我很难将 2 个函数嵌套到更大的函数中,以便这些函数将打开并读取文件并打印结果。没有错误,但结果显示为None。请让我知道如何改进代码。先感谢您!

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

1 回答 1

4

你应该完全重新考虑你是如何做到这一点的。您的代码似乎表明您不了解函数在 Python 中的工作原理。例如,这个:

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

. . . 创建三个函数,但不调用其中任何一个。

首先阅读Python 教程

于 2012-07-12T18:59:07.380 回答