88
99_registry = winreg .ConnectRegistry (None , winreg .HKEY_LOCAL_MACHINE )
1010
11- try :
12- _write_key = winreg .OpenKey (_registry , "SYSTEM\\ CurrentControlSet\\ Control\\ Session Manager" , 0 , winreg .KEY_WRITE )
13- _read_key = winreg .OpenKey (_registry , "SYSTEM\\ CurrentControlSet\\ Control\\ Session Manager" , 0 , winreg .KEY_READ )
14- except PermissionError :
15- raise PermissionError ("Permission is denied to read and/or write to Session Manager key." )
16-
17-
1811def __get_current_values ():
1912 """Get Values.
2013
@@ -24,6 +17,10 @@ def __get_current_values():
2417 str[]: List of strings in PendingFileRenameOperations
2518
2619 """
20+ try :
21+ _read_key = winreg .OpenKey (_registry , "SYSTEM\\ CurrentControlSet\\ Control\\ Session Manager" , 0 , winreg .KEY_READ )
22+ except PermissionError :
23+ raise PermissionError ("Permission Denied to read registry key." ) # Re-raise to make clear to end-user/library user
2724 file_ops_values = None
2825 i = 0
2926 while True :
@@ -48,6 +45,10 @@ def __set_registry(values):
4845 values (str[]): List of strings to write to PendingFileRenameOperations key.
4946
5047 """
48+ try :
49+ _write_key = winreg .OpenKey (_registry , "SYSTEM\\ CurrentControlSet\\ Control\\ Session Manager" , 0 , winreg .KEY_WRITE )
50+ except PermissionError :
51+ raise PermissionError ("Permission Denied to write registry key." )
5152 winreg .SetValueEx (_write_key , "PendingFileRenameOperations" , 0 , winreg .REG_MULTI_SZ , values )
5253
5354
@@ -64,9 +65,16 @@ def DeleteFile(file_path):
6465
6566 """
6667 file_path = file_path .replace ("/" , "\\ " )
67- if not (os .path .isfile (file_path )):
68- raise FileNotFoundError ("Path {} does not exist!" .format (file_path ))
6968 values = __get_current_values ()
69+ if not (os .path .isfile (file_path )):
70+ values .reverse ()
71+ try :
72+ file_path_index = values .index ("\\ ??\\ " + file_path )
73+ except IndexError :
74+ file_path_index = - 1
75+ if file_path_index % 2 != 0 or file_path_index == - 1 :
76+ raise FileNotFoundError ("Path {} does not exist and is not being created during a move operation!" .format (file_path ))
77+ values .reverse ()
7078 values .append ("\\ ??\\ " + file_path )
7179 values .append ("" )
7280 __set_registry (values )
0 commit comments