#!/usr/bin/env python
# encoding:utf-8
#author gxcc 2016
import os,sys,time
install_dir = "/soft/"   #定义存放安装包的目录
tar_dir ="/softdoc/"     #定义解压安装包存放的目录
os.chdir(install_dir)    #切换至/soft
print "\033[32m++++++++welcome to use python install script+++++++\033[0m"
time.sleep(1)
if os.path.exists(tar_dir):
    print "the doc %s exist" % tar_dir
else:
    os.mkdir(tar_dir)
    print "the doc %s create sucessful" % tar_dir
install_file  = os.listdir(install_dir)   #将安装目录下的软件包以列表的形式显示
print "=================================================================="
for i in range(len(install_file)):
    print i,install_file[i]
print "=================================================================="
while True:
    j = len(install_file)   #列表的长度
    choice = raw_input("pleas enter an number:")
    if not choice.isdigit():    #定义是否输入是纯数字
        print "please enter interger"
        break
    
    if int(choice) > j-1:    #列表的索引最大值比长度少1
        print "the index out of range"
        break
    else:
        print "Begin to exce the file"
        time.sleep(1)
        tar_cmd = "tar zxvf %s -C %s" % (install_file[int(choice)],tar_dir)
        os.system(tar_cmd)
        print "It's ok"
        break
在写脚本的时候,遇到很多问题。主要还是逻辑思维还不太够清晰。经过一番改良后,更加加深了对语法的印象。