问题
I am using windows 7 64 bit python 2.7 I am opening the file, and monitoring the file changes and then waiting till the opened file is closed. This works well in case of simple notepad file opener. As notepad opens each files in a new process ID whereas notepad++ opens different files in a single notepad++ process ID.
ACTIONS = {
1 : "Created",
2 : "Deleted",
3 : "Updated",
4 : "Renamed from something",
5 : "Renamed to something"
}
FILE_LIST_DIRECTORY = 0x0001
class myThread (threading.Thread):
def __init__(self, threadID, fileName, directory, origin):
threading.Thread.__init__(self)
self.threadID = threadID
self.fileName = fileName
self.daemon = True
self.dir = directory
self.originalFile = origin
def run(self):
startMonitor(self.fileName, self.dir, self.originalFile)
def startMonitor(fileMonitoring,dirPath,originalFile):
hDir = win32file.CreateFile (
dirPath,
FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS,
None
)
readFlags = win32con.FILE_NOTIFY_CHANGE_FILE_NAME | \
win32con.FILE_NOTIFY_CHANGE_DIR_NAME | \
win32con.FILE_NOTIFY_CHANGE_ATTRIBUTES | \
win32con.FILE_NOTIFY_CHANGE_SIZE | \
win32con.FILE_NOTIFY_CHANGE_LAST_WRITE | \
win32con.FILE_NOTIFY_CHANGE_SECURITY
# Wait for new data and call ProcessNewData for each new chunk that's written
while 1:
# Wait for a change to occur
results = win32file.ReadDirectoryChangesW (
hDir,
1024,
False,
readFlags,
None
)
# For each change, check to see if it's updating the file we're interested in
for action, file_M in results:
full_filename = os.path.join (dirPath, file_M)
#print file, ACTIONS.get (action, "Unknown")
if len(full_filename) == len(fileMonitoring) and action == 3:
#copy to main file
if os.path.exists(originalFile):
encrypt_file(key,fileMonitoring,originalFile,iv)
thread1 = myThread(1, FileName, tempLocation,selectedFileName)
thread1.start();
startupinfo = None
if os.name == 'nt':
startupinfo = subprocess.STARTUPINFO()
startupinfo.dwFlags |= subprocess._subprocess.STARTF_USESHOWWINDOW
**ss=subprocess.Popen(FileName,shell=True)**
ss.communicate()
os.remove(FileName)
So above code, it works when I close notepad++(which will close all different opened files which is not desired) instead of closing only the opened file type. When I open file with wordpad/notepad then each process of it is created with a file. Whereas with notepad++, only a single process of notepad++ carries all the different file types. So, how to invoke new process ID for different types of file openers like notepad++, msoffice, openoffice each time it opens a new fil In above case ss.communicate() is not a blocking call
来源:https://stackoverflow.com/questions/21278825/how-to-open-a-file-in-a-new-process-everytime-irrespective-of-fileopener