简体   繁体   中英

My code cannot find my file, and I am not sure what is wrong with it

def get_file():
    file_name = input("Enter the name of the file: ")
    try:
        count = 0
        total = 0.0
        average = 0.0
        maximum = 0
        minimum = 0
        range1 = 0
        with open(file_name) as file:
            number = int(line)
            count = count + 1
            total = total+ num
            maximum = max(number)
            minimum = min(number)
            average = total/count
            range = maximum = minimum
            print('The name of the file: ', file_name)
            print('The sum of the numbers: ', total)
            print('The count of how many numbers are in the file: ', count)
            print('The average of the numbers: ', average)
            print('The maximum value: ', maximum)
            print('The minimum value: ', minimum)
            print('The range of the values (maximum - minimum): ', range)
    except:
        print("The file is not found.")


def main():
    get_file()

main()

That is my code, I keep getting the error that the file is not found. I have made sure that the text files that I am inputing into this code is in the same file and that I am spelling everything right. What is wrong

well you are never doing anything with lines of file, and you need to make sure you are passing the right file path syntax to the function \ code. IE

def get_file(file_name):
    """ do something with file. """
    print(file_name, 'what path or file was sent to function?')
    try:
        with open(file_name) as file:
            for line in file:
                if line:
                    print(line)
    except FileNotFoundError:
        print("Wrong file or file path")
    except OSError as err:
        print("OS error: {0}".format(err))
    print()


def main():
    file_one = '\\192.168.1.249\DataDrive_02TB\Tools\net_scanner\hi.txt'
    file_three = 'c:\temp\hi.txt'
    file_two = '//192.168.1.249/DataDrive_02TB/Tools/net_scanner/hi.txt'
    get_file(file_one)
    get_file(file_three)
    get_file(file_two)
    file_input = input("Enter the name of the file: ")
    get_file(file_input)


main()


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