gpt4 book ai didi

python - Docker没有在KeyboardInterrupt上保存文件

转载 作者:行者123 更新时间:2023-12-02 21:14:32 25 4
gpt4 key购买 nike

我将推文存储到 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

当在 Docker 容器中运行并发出键盘中断时,它会返回
Downloaded 520 tweets, Saved to /app/tweets.csv

但什么都没有保存。

如何让脚本在容器中写出,以及这里发生了什么?

编辑:

命令运行:
docker build -t dock .
docker run dock

这是 Dockerfile:
# 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

此命令绑定(bind)您在 : 之前定义的本地目录。到目录 app在 docker 容器内。您应该检查本地目录是否存在,并且它应该是绝对路径。当您的 docker 容器在 /app 中运行时,它将在那里下载 tweets.csv,您应该可以在 /some/local/directory 中看到它完成后。当您将文件下载到与您的代码相同的目录时,您可能还会看到整个代码库。

这是 Docker volumes的链接

*为了完整起见,这与键盘中断无关。

关于python - Docker没有在KeyboardInterrupt上保存文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52031907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com