每个运行的变量之间交替

Lea*_*ava 6 python python-2.7

我想在每次运行程序时使用不同的API密钥进行数据抓取.

例如,我有以下2个键:

apiKey1 = "123abc"
apiKey2 = "345def"
Run Code Online (Sandbox Code Playgroud)

和以下网址:

myUrl = http://myurl.com/key=...
Run Code Online (Sandbox Code Playgroud)

程序运行时,我想myUrl使用apiKey1.一旦它再次运行,我会喜欢它使用apiKey2等等...即:

第一次运行:

url = "http://myurl.com/key=" + apiKey1
Run Code Online (Sandbox Code Playgroud)

第二轮:

url = "http://myurl.com/key=" + apiKey2
Run Code Online (Sandbox Code Playgroud)

对不起,如果这没有意义,但有没有人知道这样做的方法?我不知道.


编辑:

为了避免混淆,我已经看过这个答案了.但这不符合我的疑问.我的目标是在我的脚本执行之间循环变量.

zip*_*ipa 1

以下示例说明了如果不存在此类文件,如何自动创建文件:

import os
if not os.path.exists('Checker.txt'):
    '''here you check whether the file exists
    if not this bit creates it
    if file exists nothing happens'''
    with open('Checker.txt', 'w') as f:
        #so if the file doesn't exist this will create it
        f.write('0')

myUrl = 'http://myurl.com/key='
apiKeys = ["123abc", "345def"]

with open('Checker.txt', 'r') as f:
    data = int(f.read()) #read the contents of data and turn it into int
    myUrl = myUrl + apiKeys[data] #call the apiKey via index

with open('Checker.txt', 'w') as f:
    #rewriting the file and swapping values
    if data == 1:
        f.write('0')
    else:
        f.write('1')
Run Code Online (Sandbox Code Playgroud)