Python脚本控制台正常运行,Windows任务计划程序中无限运行超时
Python脚本控制台运行正常,Windows任务计划程序中无限运行超时
- 脚本功能:通过FTP下载所有zip文件,按修改日期重命名后删除服务器原文件
- 控制台直接执行脚本仅需1-2秒,表现完美
- 通过Windows任务计划程序手动触发或按计划运行时,脚本会无限运行,最终在2小时时限后超时
- 已在脚本末尾添加
exit(0),仍无法避免挂起
脚本代码如下:
import ftplib import os from pathlib import Path from datetime import datetime, timezone # FTP server details FTP_SERVER = "domain.com" FTP_USERNAME = "username" FTP_PASSWORD = "password" # Directory for downloaded files LOCAL_DIR = Path(r"G:\Neverland Backups") # Connect to the FTP server ftp = ftplib.FTP(FTP_SERVER, FTP_USERNAME, FTP_PASSWORD) # List all files in the current directory on the server files = ftp.nlst() for file in files: # Check if the file is a .zip file if file.endswith('.zip'): # Download the file local_file = LOCAL_DIR / file with open(local_file, 'wb') as fp: ftp.retrbinary('RETR ' + file, fp.write) # Get the file modification time modification_time = ftp.sendcmd('MDTM ' + file) modification_time = datetime.strptime(modification_time[4:], "%Y%m%d%H%M%S") # Rename the file renamed_filepath = LOCAL_DIR / f"Neverland_{modification_time.strftime('%Y-%m-%d_%H.%M.%S')}.zip" (LOCAL_DIR / file).rename(renamed_filepath) # Convert the modification time to local timezone modification_time = modification_time.replace(tzinfo=timezone.utc).astimezone(tz=None) # Set the file's modification time timestamp = modification_time.timestamp() os.utime(renamed_filepath, (timestamp, timestamp)) # Delete the original file on the server ftp.delete(file) # Close the connection ftp.quit() exit(0)
补充测试情况:
- 使用Windows“运行”命令执行
C:\Users\Administrator\AppData\Local\Programs\Python\Python312\python.exe "C:\scripts\download-neverland-backups.py",脚本窗口能正常打开、执行并关闭 - 任务计划程序“操作”设置截图:
内容的提问来源于stack exchange,提问作者NaturalBornCamper
相关产品推荐
相关产品推荐

