pythonw.exe can't upload file to Amazon S3

428 Views Asked by At

I've written a Python script which takes screenshot of my pc at a certain interval and send that screenshot to my S3 bucket. When I run my script with python command, it works, but when I run this script as a background task with pythonw.exe command, the screenshot capturing operation works- but nothing uploads to S3.

Here is my code:

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()

If you didn't configure your aws credentials, install the aws-cli and run the command:

aws configure
2

There are 2 best solutions below

0
On BEST ANSWER

The fname variable contains only the file name with no path information. So you're saving the file to the path named by saveas which contains full path information. When you're opening fname, it will be trying to load it from the current directory. The problem is not a difference between python.exe and pythonw.exe, but just where you're running the script from. In the first case, you must be launching the script from the SaveDirectory directory.

0
On

Thanks to Alex Taylor.

After booting up the pythonw.exe program couldn't find the current directory. The following line has been changed:

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