在工作中,要用到Python写几个脚本文件,以下为用到几个字段,特记录如下:
1、in
字符串包含关系,用in
if "hello" in "hello world":
//include
2、if
if True:
pass//待实现的内容
3、ftplib
ftp上传、下载
from ftplib import FTP
import os
ip = "127.0.0.1"
def ftp_down(filename = "fileName"):
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect(ip,'21')
ftp.login(user, password)
#print ftp.getwelcome()
bufsize = 1024
file_handler = open(filename,'wb').write
ftp.retrbinary('RETR %s' % os.path.basename(filename),file_handler,bufsize)
ftp.set_debuglevel(0)
#file_handler.close()
ftp.quit()
print "ftp down OK"
def ftp_up(filename = "fileName"):
ftp=FTP()
ftp.set_debuglevel(2)
ftp.connect(ip,'21')
ftp.login(user, password)
bufsize = 1024
file_handler = open(filename,'rb')
ftp.storbinary('STOR %s' % os.path.basename(filename),file_handler,bufsize)
ftp.set_debuglevel(0)
file_handler.close()
ftp.quit()
print "ftp up OK"
4、sys.argv
sys.argv[0]:脚本本身的名称
sys.argv[1]:脚本执行时传的第一个参数,如:test.py firstParam
len(sys.argv):计算参数个数
5、os.system
执行shell命令,如:os.system("ls -l ")
6、os.chdir
切换目录命令,等同于 shell下的cd命令
os.chdir("/usr/local")
7、print
打印命令,打印字符串
print "hello world"