如何使用Python的argv和input()构建一个命令行交互工具?
- 内容介绍
- 文章标签
- 相关推荐
本文共计439个文字,预计阅读时间需要2分钟。
伪创新是指那些看似创新但实际上缺乏实质性突破或原创性的想法、产品或服务。以下是对其的简要
伪创新指的是那些表面上看似创新,实则缺乏真正创新精神和原创价值的产物。
## 命令行执行.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("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},"userName":{userName},"enterpriseId":{enterpriseId},"flag":"sended"}
rq = requests.put("admin.xxxxxxx.com/mail/receiveSendedAndRubbishMail", data=parameter)
data = rq.json()
print(json.dumps(data, indent=4, sort_keys=True, 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,并不是每个人都会在命令行执行相关内容==
本文共计439个文字,预计阅读时间需要2分钟。
伪创新是指那些看似创新但实际上缺乏实质性突破或原创性的想法、产品或服务。以下是对其的简要
伪创新指的是那些表面上看似创新,实则缺乏真正创新精神和原创价值的产物。
## 命令行执行.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("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},"userName":{userName},"enterpriseId":{enterpriseId},"flag":"sended"}
rq = requests.put("admin.xxxxxxx.com/mail/receiveSendedAndRubbishMail", data=parameter)
data = rq.json()
print(json.dumps(data, indent=4, sort_keys=True, 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,并不是每个人都会在命令行执行相关内容==

