iwavetostars 0 Newbie Poster

Good day everyone - Since this is my first post, I must say hi.

I just started learning Python 8 hours ago and I like to challenge myself, so following a guide on playing with files, I decided to make a program that would open a .txt and it would create a backup and save to that backup in real time.

Problems - 1) CTRL+S won't do the trick, it'a mess - It could work very well if ran as daemon and only on that specifical window
2) - I wish for the script to stop running when notepad.exe, actually that specific window is closed.
Solution to 1): Get a handle of this specific window, and execute code only on IT

My guess is that I'll have to use the win32 API, which I've played with but no success whatsoever.

Here is my code:

#!/usr/bin/python27

from sys import argv
import shutil
import sys
import webbrowser
import time
import ctypes
from subprocess import *

script, filename = argv
txt=open(filename)

#############################

print "Here's your file%r" % filename
webbrowser.open(filename)

#############################

def backup():
    shutil.copyfile(filename, 'backup.txt')
    time.sleep(1)
    ### Send Keys Begin
    ctypes.windll.user32.keybd_event(0x11, 0, 0, 0) #CTRL
    ctypes.windll.user32.keybd_event(ord("S"), 0, 0, 0) #S
    ### Send Keys End

while True:
    backup()

Please keep in mind that I've just started about 8 hours ago - I'm open to advice but obviously my code needs tweaking!

I'd appreciate the help, all the best.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.