Python借助argv和input()制作命令行工具

Python借助argv和input()制作命令行工具,第1张

概述## 命令行执行.py文件并传递参数 代码示例如下,将参数解包 ``` from sys import argv import requests import json import time script, userId, userName, enterpriseId = argv parameter = {"userId":{userId},"userName":{userName},"ent

## 命令行执行.py文件并传递参数
代码示例如下,将参数解包
```
from sys import argv
import requests
import Json
import time

script,userID,username,enterpriseID = argv
parameter = {"userID":{userID},"username":{username},"enterpriseID":{enterpriseID},"flag":"sended"}
rq = requests.put("http://test.xxxxxx.com/mail/receiveSendedAndRubbishMail",data=parameter)
data = rq.Json()
print(Json.dumps(data,indent=4,sort_keys=True,ensure_ascii=False) )
```
实际命令行执行的时候输入命令:
```
python filename.py userID username enterpriseID
```
## 使用input函数逐一获取参数
```
import requests
import Json
import time
prompt = ‘>>> ‘

print("Please input ‘userID‘,For Example>>>1400")
userID = input(prompt)
print("Please input ‘username‘,For Example>>>yangdawei_10171")
username = input(prompt)
print("Please input ‘enterpriseID‘,For Example>>>10171")
enterpriseID = input(prompt)
print(f"The Program is starting to syc {userID}--{enterpriseID}--{username}‘s email and how much time is going to spend that‘s depends on how many emails in outBox,Just wait...")
before_syc_time = time.strftime(‘%Y-%m-%d %H:%M:%s‘,time.localtime(time.time()))
print("current time is" + ‘ ‘ + before_syc_time)
parameter = {"userID":{userID},"flag":"sended"}
rq = requests.put("http://admin.xxxxxxx.com/mail/receiveSendedAndRubbishMail",ensure_ascii=False) )
after_syc_time = time.strftime(‘%Y-%m-%d %H:%M:%s‘,time.localtime(time.time()))
print("current time is" + ‘ ‘ + after_syc_time)

input(‘Press Enter to exit program and then close this dialog....‘)```实际命令行执行的时候输入命令:```python filename.py```==您可以使用打包工具,例如前一篇博文中讲到pyinstaller 将其打包成.exe,并不是每个人都会在命令行执行相关内容==

总结

以上是内存溢出为你收集整理的Python借助argv和input()制作命令行工具全部内容,希望文章能够帮你解决Python借助argv和input()制作命令行工具所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1190573.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存