- 論壇徽章:
- 0
|
來(lái)源:
http://wjason.javaeye.com/blog/479695
關(guān)鍵字: python, 命令行
在公司的文件服務(wù)器上,很深的一層目錄里面有一個(gè)Excel文件,他就是我們組的日?qǐng)?bào)文件。
他的名字會(huì)隨時(shí)間的變化而變化,所以我們有辦法把它設(shè)置成我桌面的一個(gè)快捷方式。
于是這回的需求便是寫一個(gè)腳本,他會(huì)根據(jù)今天的時(shí)間,打開相對(duì)應(yīng)的日?qǐng)?bào)。
具體主要有下面這兩個(gè)功能:
1. "-d" "--directory"參數(shù)
不指定他的時(shí)候,就直接打開日?qǐng)?bào)。
指定的時(shí)候就打開日?qǐng)?bào)所在的文件就夾,因?yàn)樗哪夸浵旅孢有其他組的日?qǐng)?bào),我有時(shí)候想偷窺一下。
2. "-o" "--offset"參數(shù)
不指定他的時(shí)候,就直接打開當(dāng)前時(shí)間對(duì)應(yīng)的日?qǐng)?bào)。
指定的時(shí)候,打開往后退相應(yīng)小時(shí)數(shù)(該參數(shù)對(duì)應(yīng)的值)的時(shí)間對(duì)應(yīng)的日?qǐng)?bào),
因?yàn)槲医?jīng)常需要第二天早晨補(bǔ)充上一天的日?qǐng)?bào)。
顯然要干這么一件事情,我需要解析命令行參數(shù)了。
Python中解析命令行參數(shù)有下面三種方法。
1. 使用sys.argv
跟標(biāo)C的main函數(shù)一樣。
2. 使用getopt
跟Unix/Linux上的C函數(shù)庫(kù)getopt一樣
3. 使用OptionParser,在optparser模塊中
這個(gè)是python在模仿C的基礎(chǔ)上,增加的。對(duì)于"復(fù)雜的場(chǎng)合",使用他會(huì)更"強(qiáng)大,易用"一些。
這回就是用第三種方法,多說(shuō)無(wú)益,詳細(xì)內(nèi)容直接查看python的文檔。這里直接上程序如下:
下面程序中除了解析命令行參數(shù)以外, 其他的知識(shí)點(diǎn)有:
1. 如何引用全局變量。
2. 執(zhí)行系統(tǒng)命令。
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作業(yè)報(bào)告_20090622.xls'
filename = "PSV日?qǐng)?bào)_"+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 = "\\\\......\\日?qǐng)?bào)\\"
dest = path + rbutil.generatePsvRiBaoFileName()
if wantDir:
openFolder(path)
else:
openFile(dest)
本文來(lái)自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u2/61757/showart_2062235.html |
|