如何在循环的中间改变输入

2024-10-01 13:34:36 发布

您现在位置:Python中文网/ 问答频道 /正文

脚本在文本文件的前10行循环,并等待1分钟,然后移动到下一个10行

在移动到下一批之前,是否可以请求batchSizedelayInMinutes

import time

count = 0
hosts = open("hosts.txt", "r+")

batchSize = int(input("batchSize (default: 10) : ") or "10")
delayInMinutes = int(input("delayInMinutes (default: 1) : ") or "5")

for host in hosts:
    instanceId = host.strip()
    count = count + 1
    print(count, instanceId)
    if count % batchSize == 0:
        time.sleep(delayInMinutes * 60)
        # Test case 1
        # batchSize = int(input("batchSize (default: 10) : ") or "10")
        # delayInMinutes = int(input("delayInMinutes (default: 5) : ") or "5")
    else:
        time.sleep(3)

输出:

batchSize (default: 10) : 2
delayInMinutes (default: 5) : 1

1 05a8f8aa-8778-4cb8-b4a5-8a38700d3da6
2 06b25cee-6c0f-4516-9330-19a50f6a8f99
batchSize (default: 10) : 3
delayInMinutes (default: 5) : 1
3 0f4b033e-856b-4d75-b8fe-5e8a0d1c577d
batchSize (default: 10) : 2
delayInMinutes (default: 5) : 1
4 1545f282-d0bb-4001-b7f9-98255f2fcfaa
batchSize (default: 10) : 5
delayInMinutes (default: 5) : 1
5 2a9cc516-c75e-4e40-860b-5f77fc9a5a8a

Tags: orimport脚本defaulthostinputtimecount