当前位置: 技术问答>linux和unix
linux下如何运行带参数的python脚本
来源: 互联网 发布时间:2016-12-30
本文导语: 我在Linux用eclipse写了个发送邮件的脚本sendEmail.py,但是写好之后不知道如何调用带参数的脚本,如果不带参数的话可以用: $python *.py调用python脚本。我应该如何调用带参数的py脚本呢?我的脚本如下,如果我的脚本...
我在Linux用eclipse写了个发送邮件的脚本sendEmail.py,但是写好之后不知道如何调用带参数的脚本,如果不带参数的话可以用: $python *.py调用python脚本。我应该如何调用带参数的py脚本呢?我的脚本如下,如果我的脚本不能直接通过$python *.py的格式调用的话我应该怎样修改我的python脚本呢?
#!/usr/src/Python-3.1.2
# -*- coding: utf-8 -*-
#Function:发送带附件的邮件
#Version: 1.0
#Filename:sendEmail.py
""" receiver,subject,text or file absolute path,attached
receiver and attached file
"""
import smtplib
from email.header import Header
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib, datetime
from email.mime.base import MIMEBase
#from email.mime.text import MIMEText
from email import encoders
import os
def sendEmail(receiver,subject,text ,attachfiles,):
#receiver,subject,msg
receiver = receiver.replace(',',';')
attachfiles = attachfiles.replace(',',';')
mailSVR = 'mail.xxx.com'
sender = 'monitor@xxx.com'
PWD = 'xxx'
#创建一个带附件的实例
msg = MIMEMultipart()
#构造附件
#添加正文内容
if os.path.exists(text):
att = MIMEText(open(text,'rb').read(), 'utf8')
else:
att = MIMEText(text, 'utf8')
msg.attach(att)
#添加附件
for f in attachfiles.split(';'):
part = MIMEBase('application','octet-stream')
part.set_payload(open(f,'rb').read())
encoders.encode_base64(part)
part.add_header('Content-Disposition','attachment; filename="%s"'%os.path.basename(f))
msg.attach(part)
#加邮件头
msg['from'] = 'monitor@xxx.com'
msg['to'] = receiver
msg['subject'] = Header(subject)
handle = smtplib.SMTP(mailSVR,25)
handle.ehlo()
handle.starttls()
handle.ehlo()
handle.login(sender,PWD)
print(msg['from'])
print(receiver.split(';'))
print(msg.as_string())
handle.sendmail(msg['from'],receiver.split(';'),msg.as_string())
handle.close()
|
manage_dev(['CS', 'ES', 'RRS', 'MM', 'CMI', 'UM', 'MC'])
sys.argv
sys.argv
|
把另外这个py import进来.
然后封装一个函数去实现这个py的功能, 再把参数传给这个函数调用.
然后封装一个函数去实现这个py的功能, 再把参数传给这个函数调用.