我有一个Python脚本,当从Eclipse运行时,IDLE,双击目录文件夹中的.py文件时效果非常好。我需要自动运行这个每晚,但我不能让它运行从Windows任务调度程序,所以我写了一个.bat文件,它也不能工作。在
Python脚本:
import urllib.request
import time
from lxml import etree
import datetime
import csv
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
today = datetime.date.today()
ts = int(time.time()) - 86400
tsend = int(time.time())
#ts = 1461253877
#tsend = 1461340277
dailyReport = "URL_GOES_HERE".format(ts, tsend)
with urllib.request.urlopen(dailyReport) as url:
soup = url.read()
saveFile = open('{}_dailyIdleReport.xml'.format(today),'wb')
saveFile.write(soup)
saveFile.close()
tree = etree.parse('{}_dailyIdleReport.xml'.format(today))
root = tree.getroot()
print(root.tag, root.attrib)
zonarFile = open('{}_idleReport.csv'.format(today),'w', newline='')
outputWriter = csv.writer(zonarFile)
outputWriter.writerow(['Asset ID', 'Event Type', 'Idle Length', 'Cost'])
for assetidle in root.findall('assetidle'):
for element in assetidle:
for event in assetidle.findall('event'):
fleet = assetidle.get('fleet')
eventtype = event.get('type')
length = event.find('length').text
tlength = length
(h, m, s) = tlength.split(':')
result = ((float(h)/1) + (float(m)/60) + (float(s)/3600))
cost = (result * 1.5) * 1.80
displayCost = '${:,.2f}'.format(cost)
zonarFile = open('{}_idleReport.csv'.format(today),'a', newline='')
outputWriter = csv.writer(zonarFile)
outputWriter.writerow([fleet,eventtype,length,displayCost])
zonarFile.close()
#print('Asset #: %s %s %s %s' %(fleet,eventtype,length,displayCost))
fromaddr = "myemail@server.com"
toaddr = "myemail@server.com"
msg = MIMEMultipart()
msg['From'] = fromaddr
msg['To'] = toaddr
msg['Subject'] = "The Zonar %s" %'{}_idleReport.csv'.format(today)
body = "The Zonar Daily Idle Report is attached."
filename = "{}_idleReport.csv".format(today)
attachment = open("C:\\Users\\PeggyBall\\workspace\\ZonarCSCProject\\src\\{}_idleReport.csv".format(today), "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
body = "The Zonar Idle Report for {} is attached.".format(today)
msg.attach(MIMEText(body, 'plain'))
msg.attach(part)
server = smtplib.SMTP('smtp.email.serverhere', 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login("email_username", "email_password")
text = msg.as_string()
server.sendmail(fromaddr, toaddr, text)
当前.bat文件:
^{pr2}$上面.bat文件的CMD输出(此输出是预期的,但.xml和.csv文件从未从.bat创建):
eventlist {'end': '1461716317', 'ver': '1', 'count': '38', 'start': '1461629917'}
Press any key to continue . . .
以前的.bat文件无效:
@echo off
C:\Users\PeggyBall\workspace\ZonarCSCProject\src\DailyIdleReport.py %*
pause
@echo off
C:\Users\PeggyBall\AppData\Local\Programs\Python\Python35\python.exe C:\Users\PeggyBall\workspace\ZonarCSCProject\src\DailyIdleReport.py %*
pause
以下是错误消息:
eventlist {'ver': '1', 'end': '1461624597', 'count': '33', 'start': '1461538197'}
Traceback (most recent call last):
File "C:\Users\PeggyBall\workspace\ZonarCSCProject\src\DailyIdleReport.py", line 68, in <module>
attachment = open("C:\\Users\\PeggyBall\\workspace\\ZonarCSCProject\\src\\idleReport.csv","rb")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\PeggyBall\\workspace\\ZonarCSCProject\\src\\idleReport.csv'
Press any key to continue . . .
我已经把双倍的\改为单的\但那也没用。如有任何建议,将不胜感激。在
谢谢!在
考虑在所有文件中指定一个绝对路径。目前,在您的外部文件的
open()
中,假定相对路径在外部运行,如果批处理文件和命令行在外部运行,则会出现问题。在尝试将.xml和.csv文件保存到.py脚本的当前路径,并且对.py(IDE或命令行)的任何调用都将使用这种绝对路径。下面使用
os.path.join()
连接目录和文件名。此函数与平台无关(Windows、Mac、Linux),并且避免了反斜杠或正斜杠的需要,并且可以在部署到其他用户时使用,因为没有设置硬编码路径。在相关问题 更多 >
编程相关推荐