- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我将推文存储到 CSV 中,当我在 Jupyter Notebook 中运行以下代码时,它会成功保存到 tweets.csv。
with open(fName, 'a') as f:
while True:
try:
if (max_id <= 0):
# to the beginning of twitter time
if (not sinceId):
results = api.search(q=query, count=tweetCount)
# go to last tweet we downloaded
else:
results = api.search(q=query, since_id=sinceId, count=tweetCount)
# if max_id > 0
else:
# results from beginning of twitter time to max_id
if (not sinceId):
results = api.search(q=query, max_id=str(max_id - 1), count=tweetCount)
# results from since_id to max_id
else:
results = api.search(q=searchQuery, count=tweetCount,
max_id=str(max_id - 1),
since_id=sinceId)
if not results:
print("No more tweets found")
break
for result in results:
tweets_DF = pd.DataFrame({"text": [x.text for x in results]},
index =[x.id for x in results])
tweets_DF.name = 'Tweets'
tweets_DF.index.name = "ID"
tweets_DF.to_csv(f, header=False)
tweetCount += len(results)
print("Downloaded {0} tweets".format(tweetCount))
max_id = results[-1].id
except (KeyboardInterrupt, SystemExit):
print ("Downloaded {0} tweets, Saved to {1}".format(tweetCount, os.path.abspath(fName)))
quit()
except tweepy.TweepError as e:
print("Error : " + str(e))
break
Downloaded 520 tweets, Saved to /app/tweets.csv
docker build -t dock .
docker run dock
# Use an official Python runtime as a parent image
FROM python:3.6-slim
# Set the working directory to /app
WORKDIR /app
# Copy the current directory contents into the container at /app
ADD . /app
# Install any needed packages specified in requirements.txt
RUN pip install --trusted-host pypi.python.org -r requirements.txt
# Make port 80 available to the world outside this container
EXPOSE 80
# Define environment variable
ENV NAME World
# Run app.py when the container launches
CMD ["python", "app.py"]
enter code here
最佳答案
从您的 docker 命令,您似乎没有将本地目录安装到您的 docker 容器。 Docker 容器与主机有不同的文件系统,一旦容器被删除,保存在 docker 容器中的文件就会丢失。要解决此问题,请像这样运行您的命令:
docker run -v /User/Seth/some/local/directory:/app/ dock
:
之前定义的本地目录。到目录
app
在 docker 容器内。您应该检查本地目录是否存在,并且它应该是绝对路径。当您的 docker 容器在
/app
中运行时,它将在那里下载 tweets.csv,您应该可以在
/some/local/directory
中看到它完成后。当您将文件下载到与您的代码相同的目录时,您可能还会看到整个代码库。
关于python - Docker没有在KeyboardInterrupt上保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52031907/
代码如下: try: input() # I hit Ctrl+C except Exception as e: print(str(e)) 这是回溯: Traceback (most
我使用这段代码来中断函数执行,而没有任何错误回溯。 if __name__ == "__main__": try: main() except KeyboardInte
有人可以向我解释以下内容吗?让我们看一下代码: if __name__ == '__main__': try: while 1: x = 2+2
在下面的简单示例中,我用函数 foo() 做了一些事情。函数内部有一个变量a。我想做的是当程序捕捉到 KeyboardInterrupt 时,可以打印局部函数变量 a。 很明显,下面的方法是行不通的。
Cont = 1 while Cont == 1: try: while Cont == 1: counter = counter + 0.1
我有一个循环,在每次迭代中我(重新)计算一个长值向量: try: while(1): long_vec = recalculate( long_vec ) la
我目前正在使用 Twisted 在回调中的 for 循环中重复一项任务,但是如果用户通过 Ctrl-C 发出 KeyboardInterrupt,我希望 react 器在回调(一个)中中断循环。根据我
我有以下代码: while True: try: #DoSomething except KeyboardInterrupt: break 但是我不想使
我希望向我的代码添加功能,这样如果我想在任何时候终止代码,它都会安全地终止训练并保存变量。虽然我已经尝试寻找更好的解决方案,但我想捕获一个 KeyboardInterrupt异常(exception)
我有一个代码,可用于在 Ctrl+C 时停止。我想在我按 Ctrl+C 后做一些事情。因此我写: try: work() except KeyboardInterrupt: do_ot
我一直在搞一个 Django 项目。我想要实现的是 Django 项目在另一个进程中启动,而父进程启动我编写的任意代码加载(我的项目的后端)。显然,Django 进程和父进程进行通信。我希望进程可以读
我在编写启动本地 JBoss 实例的脚本时遇到了一个奇怪的问题。 我的代码看起来像这样: with open("/var/run/jboss/jboss.pid", "wb") as f: p
我在 Windows 上执行了以下测试代码: import multiprocessing import time def child() : while True : time.slee
我正在使用 Python 调用使用子进程模块的 C++ 程序。由于该程序需要一些时间才能运行,我希望能够使用 Ctrl+C 终止它。我在 StackOverflow 上看到了一些与此相关的问题,但似乎
from socket import socket, AF_INET, SOCK_STREAM sock = socket(AF_INET, SOCK_STREAM) sock.bind(("loca
在 Linux 上将 Tkinter 与 Python 结合使用,我试图通过使用 KeyboardInterrupt 异常让 Ctrl+C 停止执行,但是当我按下它时有一段时间没有任何反应。最终它“接
不知何故,我的 pycharm 调试器不再连接。 我测试了所有 ENV。另外,在 stackoverflow 上阅读它:我删除了 .idea 目录。 然而,在重新启动pycharm .idea 后总是
我正在制作一个简单的多线程端口扫描器。它扫描主机上的所有端口并返回打开的端口。问题在于中断扫描。扫描完成需要很长时间,有时我希望在扫描过程中用 C-c 终止程序。问题是扫描不会停止。主线程锁定在 qu
我正在运行这个简单的代码: import threading, time class reqthread(threading.Thread): def run(self):
如何使用 fabric 触发 Ctrl+C,换句话说,是否可以通过 bash 手动触发 KeyboardInterrupt? 最佳答案 ctrl+c 生成 SIGINT 信号。 您可以使用 kill
我是一名优秀的程序员,十分优秀!