-1
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

001
  • 13,291
  • 5
  • 35
  • 66
Holly
  • 1
  • 1
  • 1
    How are you running this script? Where is the file located *in relation to the location of the script?* By the way, [don't use a bare except.](https://stackoverflow.com/a/4990739/6298712) – ddejohn Mar 11 '22 at 22:53
  • I have tried running this script in both python IDLE and Visual studio code if that is what you mean. Both the script and the python file are in the same folder. I am new to this, so sorry if that is not what you mean. – Holly Mar 11 '22 at 22:55
  • 2
    `number = int(line)` Where does `line` come from? Maybe it's not a file not found exception. – 001 Mar 11 '22 at 22:55
  • aaaaaaaaaand this is why you don't use a catch-all – ddejohn Mar 11 '22 at 22:56
  • In what folder are you when you run the script? – md2perpe Mar 11 '22 at 22:56
  • That line is changing a list of numbers that are saved as a string into an integer – Holly Mar 11 '22 at 22:57
  • 1
    @Holly change your `except` to `except Exception as error; print(error)` – ddejohn Mar 11 '22 at 22:58
  • Johnny Mopp is asking about the variable called `line`, which doesn't appear to be defined anywhere. – ddejohn Mar 11 '22 at 22:58
  • 1
    Your `try/except` block assumes that the only possible error would be that the file is not found. But that is not true. – John Gordon Mar 11 '22 at 23:02
  • 2
    You should not use `try` as broadly as this. The reason why this part can fail are different ones from just the file not being found. Remove this part and you will get more helpful error messages. Probably first "File not found", then after you enter a valid file path relative to the one the file is in something like "NameError: 'line' not defined" etc. This way you can go about fixing them one by one until the program runs. And afterwards see the results and fix that it does everything correctly. – ewz93 Mar 11 '22 at 23:02
  • there are a number of errors in the code (hence the comments), the best thing to do when asking is follow this protocol: https://stackoverflow.com/help/minimal-reproducible-example it will be helpful to you and others answering. – D.L Mar 12 '22 at 08:40

1 Answers1

0

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. I.E.

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()


Bryan
  • 11
  • 3