【发布时间】:2017-01-31 09:12:27
【问题描述】:
目前我正在研究 tweepy,我需要将 tweet id 存储在一个变量中,并希望它在脚本再次运行时保留这个值。我知道我可以使用文件和数据库来做到这一点,但我想使用环境变量来做到这一点。寻找正确方向的推动力。我已经使用命令 >export en=1 从终端设置了环境变量 这是我正在使用的代码:
#!/usr/bin/env python
# encoding: utf-8
import tweepy
import time
import random
import os
t=time.time()
#my api keys
consumer_key = "xxx"
consumer_secret = "xxx"
access_key = "xxx"
access_secret = "xxx"
toReply="xxx"
rt=['hello','hi','okay','bye']
#rt=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','you','suck','bruh',':P']
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_key, access_secret)
api = tweepy.API(auth)
ts=api.user_timeline(screen_name=toReply,count=1)
#g=open('id.txt','r')
#x=g.read()
#g.close()
x=os.environ["en"]
print x
l=[]
#print dir(ts)
#print ts.max_id
#print type(ts.max_id)
def unique(y,l):
for i in range(0,len(l)):
if l[i]==y:
return 0
if int(x)!=ts.max_id:
for tweet in ts:
for i in range(0,len(rt)+1):
y=random.choice(rt)
#print y
if(unique(y,l)!=0):
#print("unique")
api.update_status("@" + toReply + " "+y, in_reply_to_status_id = tweet.id)
#print y
l.append(y)
os.environ["en"]=str(ts.max_id)
print os.environ["en"]
'''
f=open('id.txt','w')
f.write(str(ts.max_id))
f.close()
'''
t1=time.time()-t
print t1
【问题讨论】:
-
环境变量是单向的:它们从命令解释器(或其他执行环境)进入程序。它们也由该过程的后代继承。但是程序不能改变它的父环境。
-
我建议你使用一个文件,
pickle它
标签: python environment-variables