You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.5 KiB

from datetime import datetime
import win32con
import win32api
import win32gui
import shutil
import os
def printf(str):
# 获取当前时间
now = datetime.now()
# 格式化时间
current_time = now.strftime("%Y-%m-%d %H:%M:%S")
# 打印当前时间
print(current_time + ' ' + str)
def remove_dir_contents(dir_path):
# 检查目录是否存在
if not os.path.exists(dir_path):
printf(f"指定的目录不存在: {dir_path}")
return
# 遍历目录中的所有文件和子目录
for filename in os.listdir(dir_path):
file_path = os.path.join(dir_path, filename)
try:
# 如果是文件或链接,则删除
if os.path.isfile(file_path) or os.path.islink(file_path):
os.unlink(file_path)
# 如果是目录,则递归删除
elif os.path.isdir(file_path):
shutil.rmtree(file_path)
except Exception as e:
printf(f'删除文件或目录时出错: {e}')
def window_enum_handler(hwnd, resultList):
if win32gui.IsWindowVisible(hwnd) and win32gui.GetWindowText(hwnd) != '':
resultList.append((hwnd, win32gui.GetWindowText(hwnd)))
def get_app_list(handles=[]):
mlst = []
win32gui.EnumWindows(window_enum_handler, handles)
for handle in handles:
mlst.append(handle)
return mlst
# 切换剪映到第二个屏幕上
def move_window_to_second_screen(hwnd):
# 获取主监视器的分辨率
monitor_info = win32api.GetMonitorInfo(win32api.MonitorFromWindow(hwnd, win32con.MONITOR_DEFAULTTOPRIMARY))
primary_monitor_width = monitor_info['Monitor'][2]
# 假设第二个监视器在主监视器的右侧
# 获取窗口的当前位置
x, y, width, height = win32gui.GetWindowRect(hwnd)
# 计算新的位置
new_x = primary_monitor_width
new_y = y
# 设置新的位置
win32gui.SetWindowPos(hwnd, win32con.HWND_TOP, new_x, new_y, width, height, win32con.SWP_SHOWWINDOW)
def count_files(directory):
# 确保传入的是一个目录
if not os.path.isdir(directory):
print("指定的路径不是一个目录")
return 0
# 初始化文件计数器
file_count = 0
# 遍历目录
for item in os.listdir(directory):
# 获取每个项的完整路径
full_path = os.path.join(directory, item)
# 如果是文件,增加计数
if os.path.isfile(full_path):
file_count += 1
return file_count