[color=]自动运行的文件夹
,一个是
[color=]python的安装路径
,不需要任何库和依赖,我的python是虚拟环境Anaconda3 ,安装在E盘,
[color=]不需要注册表和设置环境变量
的。
会被火绒拦截,运行就行
1.输入自动运行的文件夹
[color=]运行输入就行
2.修改python的安装路径为自己安装路径
把 set "PYTHON_PATH=E:\ProgramData\Anaconda3\python.exe" 改成自己的python路径,找不到下个
[color=]everything 搜索一下
[Python] 纯文本查看 复制代码import os
def get_startup_folder():
# 获取当前用户的AppData Roaming文件夹
appdata_roaming = os.environ.get('APPDATA')
if not appdata_roaming:
raise EnvironmentError("APPDATA environment variable not found.")
# 拼接启动文件夹的路径
startup_folder = os.path.join(appdata_roaming, 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup')
return startup_folder
def create_batch_file(startup_path, folder_path):
bat_content = f"""
@echo off
setlocal
REM 设置要执行的Python文件所在的文件夹路径
set "FOLDER_PATH={folder_path}"
REM 设置Python解释器的路径
set "PYTHON_PATH=E:\ProgramData\Anaconda3\python.exe"
REM 遍历文件夹中的所有Python文件并执行
for %%f in ("%FOLDER_PATH%\*.py") do (
echo Executing %%f...
"%PYTHON_PATH%" %%f
if errorlevel 1 (
echo Failed to execute %%f.
pause
exit /b 1
)
REM 等待10秒
timeout /t 10 /nobreak >nul
)
echo All Python files have been executed.
endlocal
"""
# 指定批处理文件的路径和名称
bat_file_path = os.path.join(startup_path, "auto.bat")
# 将内容写入批处理文件
try:
with open(bat_file_path, "w", ) as bat_file:
bat_file.write(bat_content)
return bat_file_path
except Exception as e:
raise IOError(f"Failed to write batch file: {e}")
if __name__ == "__main__":
try:
startup_path = get_startup_folder()
print(f"您的自动运行路径是: {startup_path}")
# 设置Python文件所在文件夹路径
folder_path = input("请输入你的python自动运行路径: ").strip()
if not os.path.isdir(folder_path):
raise NotADirectoryError(f"The specified path is not a directory: {folder_path}")
# 获取用户确认
confirmation = input(f"您是否想在'{startup_path}'中创建一个批处理文件,以便在启动时执行'{folder_path}'中的所有Python文件? (y/n): ").strip().lower()
if confirmation != 'y':
print("Operation cancelled by the user.")
exit()
# 创建批处理文件
bat_file_path = create_batch_file(startup_path, folder_path)
print(f"Batch file '{bat_file_path}' has been created with the specified content.")
except Exception as e:
print(f"An error occurred: {e}")