通过python判断用户是否在此电脑首次登录

复制代码
    以下提供一种思路,通过在用户目录下创建的文件从而判断文件和当前日期相等,如果相等则代表是首次登录。
    在用户目录下创建firstlogging.txt文件(如果文件已存在则跳过),并比较文件的创建日期和当前系统日期, 如果相等则运行某个程序(或代码)。
    最后将python代码打包成exe,在windows系统创建任务计划。
python 复制代码
import os
import datetime
import subprocess

# 获取用户目录
user_profile = os.getenv('UserProfile')
file_path = os.path.join(user_profile, 'firstlogging.txt')

# 检查文件是否存在,如果不存在则创建
if not os.path.exists(file_path):
    with open(file_path, 'w') as f:
        f.write('This is a log file.')
# else:
   # print('File already exists.')

# 获取文件创建时间
creation_time = os.path.getctime(file_path)
creation_date = datetime.datetime.fromtimestamp(creation_time).date()

# 获取当前系统日期
current_date = datetime.datetime.now().date()

# 比较文件创建日期和当前系统日期
if creation_date == current_date:
    # 运行某个程序,例如打开记事本
    subprocess.run(['C:\\Software\\Selft_Command\\1.exe'])   # 时间相等即运行软件
    # print("文件创建日期和当前系统日期相同,执行某个操作")
相关推荐
科雷软件测试1 小时前
Python中itertools.product:快速生成笛卡尔积
开发语言·python
OOJO2 小时前
c++---list介绍
c语言·开发语言·数据结构·c++·算法·list
派大星~课堂4 小时前
【力扣-142. 环形链表2 ✨】Python笔记
python·leetcode·链表
Thomas.Sir4 小时前
第一章:Agent智能体开发实战之【初步认识 LlamaIndex:从入门到实操】
人工智能·python·ai·检索增强·llama·llamaindex
笨笨饿4 小时前
29_Z变换在工程中的实际意义
c语言·开发语言·人工智能·单片机·mcu·算法·机器人
艾为电子4 小时前
【技术帖】让接口不再短命:艾为 C-Shielding™ Type-C智能水汽防护技术解析
c语言·开发语言
ZTL-NPU4 小时前
Jetbrains开发ros
ide·python·pycharm·编辑器·ros·clion
棉花骑士5 小时前
【AI Agent】面向 Java 工程师的Claude Code Harness 学习指南
java·开发语言
IGAn CTOU5 小时前
PHP使用Redis实战实录2:Redis扩展方法和PHP连接Redis的多种方案
开发语言·redis·php
环黄金线HHJX.5 小时前
TSE框架配置与部署详解
开发语言·python