通过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("文件创建日期和当前系统日期相同,执行某个操作")
相关推荐
Lucky高1 天前
Pandas库实践3_索引
开发语言·python·pandas
Sheep Shaun1 天前
STL:string和vector
开发语言·数据结构·c++·算法·leetcode
Iridescent11211 天前
Iridescent:Day33
python
小徐Chao努力1 天前
Go语言核心知识点底层原理教程【Slice的底层实现】
开发语言·算法·golang
2301_805962931 天前
嘉立创EDA添加自己的元件和封装
java·开发语言
Rookie_explorers1 天前
go私有仓库athens搭建
开发语言·后端·golang
傻啦嘿哟1 天前
Python爬虫进阶:反爬机制突破与数据存储实战指南
开发语言·爬虫·python
2301_764441331 天前
基于Streamlit构建的风水命理计算器
开发语言·python
@Mr Wang1 天前
云服务器之使用jupyter运行ipynb文件
服务器·python·jupyter·notebook
Python私教1 天前
Jupyter是什么?如何安装使用?
ide·python·jupyter