Python程序:读取文件

时间:2017-05-31 16:10:31

标签: python python-3.x

我应该为一个程序编写代码,该程序会不断向用户询问文件名,直到输入正确的文件名。然后find_min_percent应该从GDP.txt文件中获取一个参数,一行(str)然后遍历该行以找到最小值并返回该值。到目前为止,这是我的代码

line = " " 

def open_file(): 
    ''' Repeatedly prompt until a valid file name allows the file to be opened.'''
    while True: 
        user_input = input('Enter a file name: ')
        try: 
            file = open(user_input, 'r')
            return file 
            break
        except FileNotFoundError: 
            print('Error. Please try again') 
            open_file()

def find_min_percent(line): 
    '''Find the min percent change in the line; return the value and the index.'''
    percent_lst = []
    line = file.readline(9)
    percent_lst += [line]
    percent_int = [float(i) for i in percent_lst]
    min_value = 10000
    for percent in percent_int:
        if percent < min_value:
            min_value = percent
            return min_value 


print(open_file())
print (find_min_percent(line))

我的问题在于readline()。它说变量文件是未定义的。此代码的大纲不包括“def find_min_percent(line):”部分中的文件。所以我不知道如何解决这个问题。我也无法在函数外部设置行,因为我必须在程序中稍后使用相同的行变量来读取其他行。所以我不知道该做什么,以至于它没有保留

3 个答案:

答案 0 :(得分:2)

您在一个函数中定义的变量无法从另一个函数中访问。要解决这个问题,你可以这样做(将返回的值存储在“main function”变量中并将其传递给下一个函数):

def find_min_percent(line): 
    '''Find the min percent change in the line; return the value and the index.'''
    percent_lst = []
    # You can use f from this function, as long as you don't modify it
    line = f.readline(9)
    percent_lst += [line]
    percent_int = [float(i) for i in percent_lst]
    min_value = 10000
    for percent in percent_int:
        if percent < min_value:
            min_value = percent
            return min_value 


f = open_file()
print(f)
print (find_min_percent(line))

顺便说一句,你使用line变量的方式很奇怪。它仅在find_min_percent内部使用,但在函数外部定义,甚至作为参数传递。为什么?你想要实现什么目标?

(有关访问函数外部定义的变量的帖子,请参阅here

答案 1 :(得分:0)

返回的file变量超出了函数的范围 固定代码:

line = " " 

def open_file(): 
    ''' Repeatedly prompt until a valid file name allows the file to be opened.'''
    while True: 
        user_input = input('Enter a file name: ')
        try: 
            file = open(user_input, 'r')
            return file 
            break
        except FileNotFoundError: 
            print('Error. Please try again') 
            open_file()

def find_min_percent(line,file): 
    '''Find the min percent change in the line; return the value and the index.'''
    percent_lst = []
    line = file.readline(9)
    percent_lst += [line]
    percent_int = [float(i) for i in percent_lst]
    min_value = 10000
    for percent in percent_int:
        if percent < min_value:
            min_value = percent
            return min_value 

temp=open_file()
print(temp)
print (find_min_percent(line,temp))

答案 2 :(得分:0)

另一种方法:

def open_file(): 
    ''' Repeatedly prompt until a valid file name allows the file to be opened.'''
    while True: 
        user_input = input('Enter a file name: ')
        try: 
            file = open(user_input, 'r')
            print('user_input: ', user_input)
            line = file.readline(9)
            file.close()
            return find_min_percent(line)
        except FileNotFoundError: 
            print('Error. Please try again') 
            open_file()

def find_min_percent(line): 
    '''Find the min percent change in the line; return the value and the index.'''
    percent_lst = []
#    line = file.readline(9)
    percent_lst += [line]
    percent_int = [float(i) for i in percent_lst]
    min_value = 10000
    for percent in percent_int:
        if percent < min_value:
            min_value = percent
            return min_value 


print(open_file())

请注意,我不确定find_min_percent方法的正确性。此外,如果您手动打开文件(不使用with open),您也需要明确关闭。