我在我的一个脚本中使用以下检查:
if os.path.exists(FolderPath) == False:
print FolderPath, 'Path does not exist, ending script.'
quit()
if os.path.isfile(os.path.join(FolderPath,GILTS)) == False:
print os.path.join(FolderPath,GILTS), ' file does not exist, ending script.'
quit()
df_gilts = pd.read_csv(os.path.join(FolderPath,GILTS))
Run Code Online (Sandbox Code Playgroud)
足够严格,当路径/文件不存在时,我获得以下打印:
IOError: File G:\On-shoring Project\mCPPI\Reconciliation Tool\Reconciliation Tool Project\3. Python\BootStrap\BBG\2017-07-16\RAW_gilts.csv does not exist
Run Code Online (Sandbox Code Playgroud)
告诉我,即使我添加了一个退出(),它仍继续使用该脚本.谁能告诉我为什么?
谢谢
根据文档,quit()(与site模块添加的其他功能一样)仅用于交互式使用.
因此,解决方案是双重的:
检查是否os.path.exists(os.path.join(FolderPath, GILTS)),不只是os.path.exists(FolderPath),以确保试图退出解释的代码实际上是达到了.
当然,使用sys.exit(1)(import sys在模块标题之后)暂停解释器的退出状态表示脚本出错.
也就是说,您可以考虑使用异常处理:
from __future__ import print_function
path = os.path.join(FolderPath, GILTS)
try:
df_gilts = pd.read_csv(path)
except IOError:
print('I/O error reading CSV at %s' % (path,), file=sys.stderr)
sys.exit(1)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
809 次 |
| 最近记录: |