我的代码找不到我的文件,我也不确定它出了什么问题


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

这是我的代码,我一直收到找不到文件的错误。我已经确保我输入到这个代码中的文本文件在同一个文件中,并且我拼写正确。出了什么问题

好吧,你从来没有用文件行做过任何事情,你需要确保你把正确的文件路径语法传递给函数\代码。即

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.249DataDrive_02TBToolsnet_scannerhi.txt'
file_three = 'c:temphi.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()

最新更新