我想打开文件并查找值。如果该值不存在,程序将显示错误信息并退出,否则将返回该值。如果文件不存在,它将处理IOError。这里是我的代码片断尝试除了打开文件python
def findValue(value):
try:
myfile = open(file, "r")
try:
for line in myfile:
if value in line:
line = do_something
return line
finally:
myfile.close()
except IOError:
print "File does not exist!"
sys.exit()
except None as e:
print "Value not found in file"
sys.exit()
当值不存在,我总是会得到无,我想我失去了一些东西,但不能看到它!有小费吗?
什么是“价值”? – 2016-04-29 09:47:27
Python区分大小写:'value'和'Value'是不同的。 – cdarke
好吧我修正了它,因为我在我的代码中,实际上我正在寻找其他的东西,并在这里显示示例,在我的代码中它具有不同的参数名称:) – Odin