真真搞不懂,python怎么会这样!
下面函数的open语句中的意思:
若是传入的参数为空的话,就返回file_path,否则就返回file_name.
初学python,这个理解起来的确有意思!
关于with as的语句理解,在ibm的developwork中有个廖雪峰的文章写得不错。我已经转了
def get_sm_ip(file_name=""):
"""
:param file_name:
:return:
"""
try:
file_path = os.path.join("/tmp", "readme.log")
<strong> with open(file_path if not file_name else file_name) as f:</strong>
try:
contents = pickle.load(f)
root_element = ET.fromstring(contents)
i = root_element.find("Config").find("RemoteAddress").text
except Exception as e:
print e
pass
except Exception as e:
print e
pass
return i