复制文件时使用 try/except 或 if/else
Using try/except or if/else when copying files
我有两种复制文件的方法,哪个是最pythonic/best版本的?
在阅读面向对象的教科书时,我被告知(此处解释)最好避免检查并在它们出现时处理 'exceptional situations'。我在第二个版本中使用 try
/except
的方式有效吗?
第一个版本使用 if
/else
:
if os.path.exists(dest):
print("\nCopying zipfile to {}".format(dest))
shutil.copy(self.backup_zipfile_name, dest)
else:
print("Cannot find {}.".format(dest))
第二个版本使用 try
/except
:
try:
shutil.copy(self.back_zipfile_name, dest)
except FileNotFoundError:
print("{!r} could not be found".format(dest))
绝对是第二个。
很容易将您的程序步骤视为连续的操作,中间没有其他任何事情发生。但这并不是计算机(至少是现代非嵌入式计算机)的工作方式。
在您检查路径是否存在和实际尝试写入它之间,其他一些程序很容易出现并删除它,从而导致未捕获的运行时异常。这是一个常见的并发错误。
因此,在使用文件系统、网络或您不使用的任何其他外部资源时,请始终使用 try/catch
(或者在 Python 的情况下,使用 try/except
)完全控制。
Here 是一个很好的资源,有更深入的解释。
编辑: 我认为@Jared Smiths 的回应更适合该特定用例(访问文件系统)。
正如大家所说,这取决于您,但这里是 python 文档中的内容,供您参考。
LBYL
Look before you leap. This coding style explicitly tests for pre-conditions before making calls or lookups. This style contrasts with the EAFP approach and is characterized by the presence of many if statements.
In a multi-threaded environment, the LBYL approach can risk introducing a race condition between “the looking” and “the leaping”. For example, the code, if key in mapping: return mapping[key] can fail if another thread removes key from mapping after the test, but before the lookup. This issue can be solved with locks or by using the EAFP approach.
EAFP
Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.
我有两种复制文件的方法,哪个是最pythonic/best版本的?
在阅读面向对象的教科书时,我被告知(此处解释)最好避免检查并在它们出现时处理 'exceptional situations'。我在第二个版本中使用 try
/except
的方式有效吗?
第一个版本使用 if
/else
:
if os.path.exists(dest):
print("\nCopying zipfile to {}".format(dest))
shutil.copy(self.backup_zipfile_name, dest)
else:
print("Cannot find {}.".format(dest))
第二个版本使用 try
/except
:
try:
shutil.copy(self.back_zipfile_name, dest)
except FileNotFoundError:
print("{!r} could not be found".format(dest))
绝对是第二个。
很容易将您的程序步骤视为连续的操作,中间没有其他任何事情发生。但这并不是计算机(至少是现代非嵌入式计算机)的工作方式。
在您检查路径是否存在和实际尝试写入它之间,其他一些程序很容易出现并删除它,从而导致未捕获的运行时异常。这是一个常见的并发错误。
因此,在使用文件系统、网络或您不使用的任何其他外部资源时,请始终使用 try/catch
(或者在 Python 的情况下,使用 try/except
)完全控制。
Here 是一个很好的资源,有更深入的解释。
编辑: 我认为@Jared Smiths 的回应更适合该特定用例(访问文件系统)。
正如大家所说,这取决于您,但这里是 python 文档中的内容,供您参考。
LBYL
Look before you leap. This coding style explicitly tests for pre-conditions before making calls or lookups. This style contrasts with the EAFP approach and is characterized by the presence of many if statements.
In a multi-threaded environment, the LBYL approach can risk introducing a race condition between “the looking” and “the leaping”. For example, the code, if key in mapping: return mapping[key] can fail if another thread removes key from mapping after the test, but before the lookup. This issue can be solved with locks or by using the EAFP approach.
EAFP
Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.