python 退出功能不工作
python quit function not working
我正在我的一个脚本中使用以下检查:
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))
奇怪的是,当 path/file 不存在时,我得到以下打印:
IOError: File G:\On-shoring Project\mCPPI\Reconciliation Tool\Reconciliation Tool Project. Python\BootStrap\BBG17-07-16\RAW_gilts.csv does not exist
告诉我,即使我添加了 quit(),脚本仍在继续。谁能告诉我为什么?
谢谢
根据 the documentation,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)
我正在我的一个脚本中使用以下检查:
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))
奇怪的是,当 path/file 不存在时,我得到以下打印:
IOError: File G:\On-shoring Project\mCPPI\Reconciliation Tool\Reconciliation Tool Project. Python\BootStrap\BBG17-07-16\RAW_gilts.csv does not exist
告诉我,即使我添加了 quit(),脚本仍在继续。谁能告诉我为什么?
谢谢
根据 the documentation,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)