How to get computer username using Python
1. The getpass module that comes with Python can be used to obtain the password entered by the user, but it can also be used to obtain the computer user name.
import getpass
user = getpass.getuser()
print("计算机用户名为:", user)
2. Use the os module to obtain the user name
Python's os module provides many methods for obtaining system information, including methods for obtaining computer user names.
import os
user = os.environ.get('USERNAME') # Windows 系统
# user = os.getlogin() # Unix/Linux 系统
print("计算机用户名为:", user)
3. Use the psutil module to obtain the user name
psutil is a cross-platform system information library in Python, which can obtain system information, process information, etc., and can also be used to obtain computer user names.
import psutil
user = psutil.users()[0][0]
print("计算机用户名为:", user)
4. Use the socket module to obtain the user name
In addition to the above three methods, the socket module of Python can also obtain the computer user name.
import socket
user = socket.gethostname()
print("计算机用户名为:", user)
Get startup folder path
startup_path = fr'C:\Users\{getpass.getuser()}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup'
print(os.listdir(startup_path))
Windows adds self-starting files
import winreg
def add_to_startup(file_path): # 要启动的文件入口绝对路径
key = winreg.HKEY_CURRENT_USER
key_value = "Software\Microsoft\Windows\CurrentVersion\Run"
with winreg.OpenKey(key, key_value, 0, winreg.KEY_WRITE) as key:
winreg.SetValueEx(key, "my_python_script", 0, winreg.REG_SZ, file_path)
# winreg.DeleteValue(key, "my_python_scrip") # 清除
winreg.CloseKey(key)
# path = r'SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run'
# key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, path, 0, winreg.KEY_WRITE)
# winreg.SetValueEx(key, 'MyProgram', 0, winreg.REG_SZ, file)
# # winreg.DeleteValue(key, 'MyProgram')
# key.Close()
Go through the startup folder to add
import getpass
from win32com.client import Dispatch
def create_systemc_start(lnk_name, exe_abs_path):
'''
:param lnk_name: 快捷方式名称
:param exe_abs_path: 要创建的exe绝对路径
:return:
'''
startup_path = fr'C:\Users\{getpass.getuser()}\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup'
start_list = os.listdir(startup_path)
if lnk_name not in start_list:
shell = Dispatch('WScript.Shell')
shortcut = shell.CreateShortCut(os.path.join(startup_path, lnk_name))
shortcut.Targetpath = exe_abs_path
shortcut.save()
Manually set the file to start automatically
1. Win + R to open and run, enter: shell:startup and copy the program file or shortcut to be started automatically to the opened window. This method is to open the Start Menu - Programs - Startup folder.
2. Win + R to open and run, enter: regedit.exe to enter the registry. After opening the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run location, right-click to create a new string value in the blank space, and fill in the location of the application that needs to be started automatically at the location of the value data.
3. Win + R to open and run, enter: gpedit.msc to enter the local policy group, double-click to start in Computer Configuration - Windows Settings - Script (Startup/Shutdown), and add the corresponding script. Need to write bat script first, need to be able to write script code
First write a bat script to execute your exe file: (create a new notepad, copy the following code, save, modify the suffix to bat)
@echo off
start exe路径
4. Right-click on My Computer to manage, or right-click on the window icon to manage Computer, under Computer Management, under Task Scheduler Library, click Create Basic Task to perform setting operations.
5. Win + R to open and run, enter: cmd, use the cmd command to add the program to the system service, and turn it on automatically
Add service command:
sc create 服务名称 binPath= 路径 start= auto
After the addition is successful, you can view the management in the system service management
Note: There is a space after the '=' in the command
Windows cmd command to create system service
sc create 命令创建一个服务(增加到注册表中)
Double-click to run directly.
As follows: Execute in command line mode:
sc create TestService binpath= "c:/in estapp.exe" displayname= "TestService" depend= Tcpip start= auto
Note: The format here,
note:
1) There must be a space after .binPath=, otherwise an error will occur.
2). If the build is wrong or needs to be modified, you need to delete the service first, and then recreate it. The command to delete the service:
sc delete [serviceName]
After prompting that the establishment is successful, you can directly enter "net start TestService"
to start the service, or you can directly start it in the "Service" of "Management Tools". (services.msc start)
C:/Documents and Settings/Administrator>sc create
Description:
Create service entries in the registry and service database.
usage:
sc <server> create [service name] [binPath= ] <option1> <option2>...