Taking screenshots with PIL
<p>I'm working on script that take screenshot every 1 min but there is one problem that when I save it gives me invalid argument and I don't know why that is.</p>

<p>Code:</p>

<pre><code>from PIL import ImageGrab
from PIL import Image
import time
import datetime
import os

def screenShot():
    while True:
        try:
            date = str(datetime.date.today())
            hour=str(datetime.datetime.now().strftime("%H"))
            os.makedirs("D:/mine/" + date + "/" + hour)
        except OSError,e:
            if e.errno != 17:
                raise
            time.sleep(1)
            pass
        while True:
            date = str(datetime.date.today())
            hour = str(datetime.datetime.now().strftime("%H"))
            date_time = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
            img = ImageGrab.grab()
            FILES_DIR = 'mine'
            SAVE_PATH = "D:/"
            LOGFILE_NAME = "%s.png" % date_time
            LOGFILE_PATH = os.path.join(SAVE_PATH, FILES_DIR, LOGFILE_NAME)
            img.save(LOGFILE_PATH)
            break
        time.sleep(60)

screenShot()
</code></pre>

<p>This is the error I'm getting:</p>

<pre class="lang-none prettyprint-override"><code>IOError: [Erno 22] Invalid argument: 'D:/mine\\2016-12-02 20:24:37.png'
</code></pre>

<p>I searched a lot and I found this code and it worked but I can't see different between this code and my code.</p>

<pre><code>import os
import sys
import time
from PIL import ImageGrab
from PIL import Image
from os import environ
import random
n = -1
while True:
    n += 1
    # generate a random time between 120 and 300 sec
    random_time = random.randrange(1,2)
    # wait between 120 and 300 seconds (or between 2 and 5 minutes)
    print "Next picture in: %.2f minutes" % (float(random_time) / 60)
    time.sleep(random_time)
    img = ImageGrab.grab()
    FILES_DIR = 'mine'
    SAVE_PATH = "D:/"
    #SAVE_PATH = os.path.expanduser("~")    #It is cross-platform
    LOGFILE_NAME = "test{n:0&gt;5}.png".format(n = n)
    LOGFILE_PATH = os.path.join(SAVE_PATH, FILES_DIR, LOGFILE_NAME)
    print LOGFILE_PATH
    img.save(LOGFILE_PATH)
</code></pre>

<p>I'm forgetting something but I don't know what is it.</p>