pythonw.exe 无法将文件上传到 Amazon S3

标签 python boto3 pythonw

我编写了一个 Python 脚本,它以一定的时间间隔截取我的电脑屏幕截图,并将该屏幕截图发送到我的 S3 存储桶。当我使用 python 命令运行我的脚本时,它可以工作,但是当我使用 pythonw.exe 命令将该脚本作为后台任务运行时,屏幕截图捕获操作可以工作 - 但没有任何内容上传到 S3。

这是我的代码:

import os
import sys
import time
import Image
import ImageGrab
import getpass
import boto3
import threading
from random import randint


s3 = boto3.resource('s3')
username =  getpass.getuser()


#---------------------------------------------------------
#User Settings:
SaveDirectory=r'C:\Users\Md.Rezaur\Dropbox\Screepy_App\screenshot'
ImageEditorPath=r'C:\WINDOWS\system32\mspaint.exe'

def capture_and_send():
    interval = randint(10,30)
    threading.Timer(interval, capture_and_send).start ()
    img=ImageGrab.grab()
    saveas=os.path.join(SaveDirectory,'ScreenShot_'+time.strftime('%Y_%m_%d_%H_%M_%S')+'.jpg')
    fname = 'ScreenShot_'+time.strftime('%Y_%m_%d_%H_%M_%S')+'.jpg'
    img.save(saveas, quality=50, optimize=True)
    editorstring='""%s" "%s"'% (ImageEditorPath,saveas) 
    data = open(fname, 'rb')
    s3.Bucket('screepy').put_object(Key=username+'/'+fname, Body=data)

capture_and_send()

如果您没有配置 aws 凭证,请安装 aws-cli 并运行命令:

aws configure

最佳答案

感谢亚历克斯·泰勒。

启动后pythonw.exe程序找不到当前目录。 以下行已更改:

data = open(SaveDirectory+'\\'+fname, 'rb')

关于pythonw.exe 无法将文件上传到 Amazon S3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32031975/

相关文章:

python - Pandas 造型师。如何忽略呈现的 HTML 中的索引列

python - 使用 OpenCV 自动调整一张纸的彩色照片的对比度和亮度

python - 查找集合是集合列表中的子集的次数

python - 如何使用 pytorch 列出所有当前可用的 GPU?

amazon-web-services - 在过去两个小时内检查 S3 存储桶中是否有新文件

amazon-dynamodb - 是否可以在 update_item 中结合 if_not_exists 和 list_append

python - 通过将凭据作为参数传递,使用 boto3 从 S3 Bucket 下载文件

windows - 在 PsychoPy 中使用 pyo 时 pythonw.exe 崩溃

python - BaseHTTPRequestHandler 在由 pythonw.exe 3.1 运行时挂起

python - 如何在退出时停止Python崩溃