免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3946 | 回复: 0
打印 上一主题 下一主题

Python 运行命令行 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-28 22:00 |只看该作者 |倒序浏览
来源:
http://wjason.javaeye.com/blog/479695


关键字: python, 命令行
在公司的文件服务器上,很深的一层目录里面有一个Excel文件,他就是我们组的日报文件。

他的名字会随时间的变化而变化,所以我们有办法把它设置成我桌面的一个快捷方式。
于是这回的需求便是写一个脚本,他会根据今天的时间,打开相对应的日报。

具体主要有下面这两个功能:
1. "-d"  "--directory"参数
不指定他的时候,就直接打开日报。
指定的时候就打开日报所在的文件就夹,因为他的目录下面还有其他组的日报,我有时候想偷窥一下。

2. "-o"  "--offset"参数
不指定他的时候,就直接打开当前时间对应的日报。
指定的时候,打开往后退相应小时数(该参数对应的值)的时间对应的日报,
因为我经常需要第二天早晨补充上一天的日报。

显然要干这么一件事情,我需要解析命令行参数了。

Python中解析命令行参数有下面三种方法。
1. 使用sys.argv
    跟标C的main函数一样。

2. 使用getopt
    跟Unix/Linux上的C函数库getopt一样

3. 使用OptionParser,在optparser模块中
    这个是python在模仿C的基础上,增加的。对于"复杂的场合",使用他会更"强大,易用"一些。

这回就是用第三种方法,多说无益,详细内容直接查看python的文档。这里直接上程序如下:

下面程序中除了解析命令行参数以外, 其他的知识点有:
1. 如何引用全局变量。
2. 执行系统命令。


Python代码

  • #! /usr/bin/python   
  • #coding:utf-8   
  •   
  • __author__="wjason"  
  • __date__ ="$2009-6-10 11:32:31$"  
  •   
  • import os,time,datetime   
  • import sys   
  •   
  • def openFile(dest):   
  •     cmd = 'cmd /C call "'+dest+'"''"'
  •     #os.popen(cmd)
  •     #os.system(cmd)
  •     import subprocess
  •     subprocess.Popen(cmd, shell=True)

  • def openFolder(dest):
  •     cmd = 'cmd /C call explorer "'+dest+'"'  
  •     #os.popen(cmd)   
  •     #os.system(cmd)   
  •     import subprocess   
  •     subprocess.Popen(cmd, shell=True)   
  •   
  • class RiBaoUtils():      
  •     def __init__(self, hour_delay = 0):   
  •         self.__hour_delay = hour_delay   
  •         self.__ct= datetime.datetime.now()   
  •         self.__ct = self.__ct - datetime.timedelta(hours= self.__hour_delay)   
  •   
  •     def generatePsvRiBaoFileName(self):   
  •         #startDay = self.__ct + datetime.timedelta(days= (0 - week))   
  •         startDay = self.__ct   
  •   
  •         # 'UtilityTeam use full year name, for example 2009.'   
  •         year =  startDay.strftime("%Y")   
  •         month =  startDay.strftime("%m")   
  •   
  •         #example: 'UtilityTeam作業報告_20090622.xls'   
  •         filename = "PSV日報_"+year+"." + month + ".xls"  
  •         return filename   
  •   
  • # -------------------------------- config start  --------------------------------------   
  • offset= 8.5  
  • wantDir = False  
  •   
  • def configOption():   
  •     from optparse import OptionParser   
  •     usage = "usage: %prog [-option]"  
  •     parser = OptionParser(usage)   
  •     parser.add_option("-d", "--directory", dest = "wantDir", action = "store_true",   
  •                       help = "use this option to open the directory.")   
  •     parser.add_option("-o", "--offset", type="float", dest="offset",   
  •                       help = "use this option to set the offset(delay) hours")   
  •   
  •     (options, args) = parser.parse_args()   
  •   
  •     if options.wantDir:   
  •         global wantDir   
  •         wantDir = options.wantDir   
  •   
  •     if  ( not options.offset == None):   
  •         global offset   
  •         offset= options.offset   
  •   
  • # -------------------------------- config end    --------------------------------------   
  •   
  • if __name__ == "__main__":   
  •     configOption()   
  •   
  •     # this off set is hours   
  •     rbutil = RiBaoUtils(offset)   
  •   
  •     #psv team, 4 jason   
  •     path = "\\\\......\\日報\\"  
  •     dest = path + rbutil.generatePsvRiBaoFileName()   
  •       
  •     if wantDir:   
  •         openFolder(path)   
  •     else:   
  •         openFile(dest)  

    本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/61757/showart_2062235.html
  • 您需要登录后才可以回帖 登录 | 注册

    本版积分规则 发表回复

      

    北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
    未成年举报专区
    中国互联网协会会员  联系我们:huangweiwei@itpub.net
    感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

    清除 Cookies - ChinaUnix - Archiver - WAP - TOP