简体   繁体   中英

Function work when executed by themselves but when all of them are called to work at once , it seem only the first one receives the argument

I have a folder with multiple text files and I am supposed to get the mean, highest and lowest of the numbers in each file. I made one function to loop through the file names, open the files and pass the data to four functions one for the mean, one for highest and one for the lowest and finally one for length. The functions work fine if I call only one one them (manually adding # in-front of the other three to see what was wrong). Say if I mute the first three and try the fourth it work, if i mute all except the second using # manually it works. But if I unmute all four, the second function says max() arg is an empty sequence

def second_part():
    for file_name in os.listdir():
        if file_name.startswith('NENE'):
            data_in_the_file = open(file_name)
            pass_to_mean = get_mean(data_in_the_file)
            pass_to_highest = get_highest(data_in_the_file)
            pass_to_lowest = get_lowest(data_in_the_file)
            pass_to_length_warning = get_length(data_in_the_file)
            print(file_name,pass_to_lowest,pass_to_mean,pass_to_highest,pass_to_length_warning)

def get_mean(my_file):
    my_list = []
    for my_line in my_file:
        my_line = my_line.strip()
        my_line = float(my_line)
        my_list.append(my_line)
    my_mean = sum(my_list)/len(my_list)
    return my_mean

def get_highest(my_file):
    my_list = []
    for my_line in my_file:
        my_line = my_line.strip()
        my_line = float(my_line)
        my_list.append(my_line)
    my_highest = max(my_list)
    return my_highest

def get_lowest(my_file):
    my_list = []
    for my_line in my_file:
        my_line = my_line.strip()
        my_line = float(my_line)
        my_list.append(my_line)
    my_lowest = min(my_list)
    return my_lowest


def get_length(my_file):
    my_list = []
    for my_line in my_file:
        my_line = my_line.strip()
        my_line = float(my_line)
        my_list.append(my_line)
    length = len(my_list)
    x = 'This is not long enough'
    y = 'This is long enough'
    if length <= 300:
        return x
    elif length >= 300:
        return y
        
second_part()

You can only iterate through an open file once. Therefore, you should store its contents in a list and pass that list around instead of needlessly iterating repeatedly.

You should do:

from pathlib import Path
def _process_file(filename):
    with open(filename, 'r') as f:
        data = [float(line.strip()) for line in f]
    
    _N = len(data)
    _mean = sum(data) / _N
    _max = max(data)
    _min = min(data)
    if _N < 300:
        file_is = 'too short'
    else:
        file_is = 'long enough'
    
    print(file_name, _min , _mean, _max, file_is)

def process_directory(directory):
    for filename in Path(directory).glob('*'):
        if filename.stem.startswith('NENE'):
            _process_file(filename)

   

   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM