-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathfile.py
More file actions
92 lines (66 loc) · 2.6 KB
/
file.py
File metadata and controls
92 lines (66 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import ctypes
import os
import time
from configparser import ConfigParser
def path_exists(path):
return os.path.exists(path)
def write_ini(filename, section, key, value):
exists = os.path.exists(filename)
cfg = ConfigParser()
if exists:
cfg.read(filename, encoding="utf-8-sig")
if not cfg.has_section(section) and not cfg.has_option(section, key):
cfg.add_section(section)
cfg.set(section, key, str(value))
with open(filename, 'w') as configfile:
cfg.write(configfile)
def read_ini(filename, section, key) -> str:
cfg = ConfigParser()
cfg.read(filename, encoding="utf-8-sig")
if cfg.has_section(section) and cfg.has_option(section, key):
return cfg.get(section, key)
return ""
def force_delete_file(file_path: str):
"""
强制删除文件
:param file_path: 文件路径
:return:
"""
kernel32 = ctypes.windll.kernel32
# 使用unicode字符串
CreateDirectory = kernel32.CreateDirectoryW
MoveFile = kernel32.MoveFileW
DeleteFile = kernel32.DeleteFileW
RemoveDirectory = kernel32.RemoveDirectoryW
class SECURITY_ATTRIBUTES(ctypes.Structure):
_fields_ = [
("Length", ctypes.c_ulong),
("SecurityDescriptor", ctypes.c_void_p),
("InheritHandle", ctypes.c_ulong),
]
temp_dir = os.path.join(os.environ["TEMP"], str(int(time.time())))
temp_dir_wchar = ctypes.c_wchar_p(temp_dir)
sa = SECURITY_ATTRIBUTES()
sa.Length = ctypes.sizeof(SECURITY_ATTRIBUTES)
sa.SecurityDescriptor = None
sa.InheritHandle = 0
res1 = CreateDirectory(temp_dir_wchar, ctypes.pointer(sa))
res2 = CreateDirectory(ctypes.c_wchar_p(temp_dir + "\\....\\"), ctypes.pointer(sa))
res3 = MoveFile(ctypes.c_wchar_p(file_path), ctypes.c_wchar_p(temp_dir + "\\....\\TemporaryFile"))
res4 = MoveFile(ctypes.c_wchar_p(temp_dir + "\\....\\"), ctypes.c_wchar_p(temp_dir + "\\TemporaryFile"))
res5 = DeleteFile(ctypes.c_wchar_p(temp_dir + "\\TemporaryFile\\TemporaryFile"))
res6 = RemoveDirectory(ctypes.c_wchar_p(temp_dir + "\\TemporaryFile"))
res7 = RemoveDirectory(temp_dir_wchar)
print(res1, res2, res3, res4, res5, res6, res7)
def main():
# file_path = "C:\\TPqd640.sys" # 更改为实际的文件路径
# force_delete_file(file_path)
# print("文件已成功删除")
cfg_name = "C:\\666.ini"
conf_exists = path_exists(cfg_name)
if not conf_exists:
write_ini(cfg_name, "default", "count", "1")
complete_number = read_ini(cfg_name, "default", "count")
print(complete_number)
if __name__ == "__main__":
main()