python argparse模块_python中argparse模块的使用

有两个文件一个是

文件1:sync_shop_source_bimer.sh

文件2:sync_shop_source_bimer.py

在sync_shop_source_bimer.sh 中调用sync_shop_source_bimer.py

文件1中产生的文件要传递给文件2

文件1中的内容

shop_flow_output="/tmp/shop_flow_source"

shop_gmv_output="/tmp/shop_gmv_source"

yesday=`date -d"-1day" +"%Y-%m-%d"`

/usr/local/bin/python sync_shop_source_bimer.py  --date $yesday --flow_file $shop_flow_output --deal_file $shop_gmv_output

文件2中接收文件

import datetime

import argparse

def get_args():

parser = argparse.ArgumentParser()

parser.add_argument('-d', '--date', help='date to stat, format: 2015-03-01', default = datetime.datetime.now().strftime("%Y-%m-%d"))

parser.add_argument('-f', '--flow_file', help='flow文件')

parser.add_argument('-g', '--deal_file', help='gmv文件')

args = parser.parse_args()

return args

def main():

args = get_args()

#打印接收参数

print dt = datetime.datetime.strptime(args.date, "%Y-%m-%d")

print flow_shop = args.flow_file

print gmv_shop = args.deal_file

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python argparse 模块Python 标准库用于解析命令行参数和选项的模块。它提供了一种简单而灵活的方法来处理命令行参数,并且可以自动生成格式漂亮的帮助文档。 argparse 有许多参数选项,其一个是 type。type 参数指定了命令行参数的数据类型。当我们使用 argparse 解析命令行参数时,它会将字符串类型的参数转换为指定的类型。例如,如果我们指定 type=int,那么 argparse 将会把输入的字符串转换为整数类型。 argparse 提供了一些内置的类型函数,如 int、float、str、bool 等,也可以自定义类型函数。下面是一个示例,说明如何使用 type 参数来指定参数的数据类型: ```python import argparse parser = argparse.ArgumentParser() parser.add_argument('--name', type=str, help='name of the user') parser.add_argument('--age', type=int, help='age of the user') args = parser.parse_args() print(args.name) print(args.age) ``` 在上面的示例,我们使用 add_argument() 方法添加了两个参数:--name 和 --age。type 参数分别指定了它们的数据类型为 str 和 int。 当我们在命令行输入参数时,argparse 会自动将字符串类型的参数转换为指定的类型。例如,我们执行以下命令: ```bash python test.py --name Tom --age 25 ``` argparse 将会把 name 参数解析为字符串类型的 'Tom',将 age 参数解析为整数类型的 25。 总之,argparse 的 type 参数让我们可以轻松地控制命令行参数的数据类型,使得命令行参数处理变得更加简单和方便。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值