退出命令行后如何在python中运行函数

时间:2022-08-11 02:40:06

I want to run a function after I "command c" on terminal. I am scrapping some data onto a file using python, and then I want to immediately close and push the file to google drive when I terminate the scrapping process.I have written two different files for scrapping and pushing but I would like to do this all in one file. How do I indicate the change of "command c" in python? If command c..do this...

我想在终端上“命令c”之后运行一个函数。我正在使用python将一些数据丢弃到文件上,然后我想立即关闭并将文件推送到谷歌驱动器,当我终止报​​废过程时。我已经写了两个不同的文件用于报废和推送但我想这样做所有在一个文件中。如何在python中指示“命令c”的变化?如果命令c..do这...

Any help would be super useful! Thanks!

任何帮助都会非常实用!谢谢!

2 个解决方案

#1


0  

The sequence ctrl+C (or command+C on Mac) causes a KeyboardInterrupt exception to be raised. You can simply catch this exception, with a simple try-except.

序列ctrl + C(或Mac上的命令+ C)会引发KeyboardInterrupt异常。您可以通过简单的try-except简单地捕获此异常。

For instance, the following code will run a dummy work that takes some time, and on a ctrl+C, will stop that work and print its current state.

例如,下面的代码将运行需要一些时间的虚拟工作,并且在ctrl + C上,将停止该工作并打印其当前状态。

import time

try:
    for i in range(100):
        time.sleep(1)
except KeyboardInterrupt:
    print(i)

Note that, as specified in the doc, the KeyboardInterrupt exception inherits from BaseException but not from Exception. As a consequence, a except Exception clause would not catch a KeyboardInterrupt, while a except BaseException would catch both KeyboardInterrupt and Exception.

请注意,如文档中所指定,KeyboardInterrupt异常继承自BaseException,但不是来自Exception。因此,除了Exception子句不会捕获KeyboardInterrupt,而除了BaseException之外,它将捕获KeyboardInterrupt和Exception。

#2


0  

What you need is atexit module https://docs.python.org/2/library/atexit.html create a handler function to do the persisting part. register it with atexit to run at the exit.

你需要的是atexit模块https://docs.python.org/2/library/atexit.html创建一个处理函数来执行持久化部分。用atexit注册它以在出口处运行。

def goodbye(name, adjective):
    print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)

import atexit
atexit.register(goodbye, 'Donny', 'nice')

#1


0  

The sequence ctrl+C (or command+C on Mac) causes a KeyboardInterrupt exception to be raised. You can simply catch this exception, with a simple try-except.

序列ctrl + C(或Mac上的命令+ C)会引发KeyboardInterrupt异常。您可以通过简单的try-except简单地捕获此异常。

For instance, the following code will run a dummy work that takes some time, and on a ctrl+C, will stop that work and print its current state.

例如,下面的代码将运行需要一些时间的虚拟工作,并且在ctrl + C上,将停止该工作并打印其当前状态。

import time

try:
    for i in range(100):
        time.sleep(1)
except KeyboardInterrupt:
    print(i)

Note that, as specified in the doc, the KeyboardInterrupt exception inherits from BaseException but not from Exception. As a consequence, a except Exception clause would not catch a KeyboardInterrupt, while a except BaseException would catch both KeyboardInterrupt and Exception.

请注意,如文档中所指定,KeyboardInterrupt异常继承自BaseException,但不是来自Exception。因此,除了Exception子句不会捕获KeyboardInterrupt,而除了BaseException之外,它将捕获KeyboardInterrupt和Exception。

#2


0  

What you need is atexit module https://docs.python.org/2/library/atexit.html create a handler function to do the persisting part. register it with atexit to run at the exit.

你需要的是atexit模块https://docs.python.org/2/library/atexit.html创建一个处理函数来执行持久化部分。用atexit注册它以在出口处运行。

def goodbye(name, adjective):
    print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)

import atexit
atexit.register(goodbye, 'Donny', 'nice')