我有一个python脚本,该脚本不断从Twitter抓取数据并将消息写入文件.我的问题是每小时,我希望程序将当前时间写入文件.下面是我的脚本.当前,它进入了时间戳功能,并一直保持每10秒打印一次时间.

#! /usr/bin/env python

import tweetstream

import simplejson

import urllib

import time

import datetime

import sched

class twit:

def __init__(self,uname,pswd,filepath):

self.uname=uname

self.password=pswd

self.filepath=open(filepath,"wb")

def main(self):

i=0

s = sched.scheduler(time.time, time.sleep)

output=self.filepath

#Grab every tweet using Streaming API

with tweetstream.TweetStream(self.uname, self.password) as stream:

for tweet in stream:

if tweet.has_key("text"):

try:

#Write tweet to file and print it to STDOUT

message=tweet['text']+ "\n"

output.write(message)

print tweet['user']['screen_name'] + ": " + tweet['text'], "\n"

################################

#Timestamp code

#Timestamps should be placed once every hour

s.enter(10, 1, t.timestamp, (s,))

s.run()

except KeyError:

pass

def timestamp(self,sc):

now = datetime.datetime.now()

current_time= now.strftime("%Y-%m-%d %H:%M")

print current_time

self.filepath.write(current_time+"\n")

if __name__=='__main__':

t=twit("rohanbk","cookie","tweets.txt")

t.main()

无论如何,我的脚本是否可以执行此操作,而无需通过IF语句每隔一分钟不断检查时间来查看已花费了多少时间?我可以像上面所做的那样使用计划的任务,对当前的实现进行一些修改吗?

解决方法:

您的代码

sc.enter(10, 1, t.timestamp, (sc,)

要求在10秒内再次安排.如果您希望每小时安排一次,

sc.enter(3600, 1, t.timestamp, (sc,)

似乎更好,因为一个小时为3600秒,而不是10秒!

还行

s.enter(1, 1, t.timestamp, (s,))

每写一条推文后,都会在1秒钟后获得时间戳记-这有什么意义?只需在循环外安排一次第一次时间戳的调用,并将其周期从10秒更改为3600.

标签:file-io,python

python 文件时间戳_在Python中每小时将时间戳记写入文件相关推荐

  1. python用时间戳给文件命名-在Python中每小时将时间戳记写入文件

    我有一个python脚本,该脚本不断从Twitter抓取数据并将消息写入文件.我的问题是每小时,我希望程序将当前时间写入文件.下面是我的脚本.当前,它进入了时间戳功能,并一直保持每10秒打印一次时间. ...

  2. python添加时间戳_在python中添加时间戳

    这两个解决方案(AFAIK)都可以在python的任何2.x版本上运行(因此保证了相当多的向后兼容性) 仅依赖于regex库的实现:import re data = ''' 2011-03-07 0: ...

  3. php图片怎么存入指定文件夹_在PHP中如何把图片存入数据库和文件夹里

    $file_dir = "your dir"; //用你的上传目录代替 $tmep_cfile1 = "goods_file".$i; $temp_cf = & ...

  4. python交互式和文件式_使用Python创建和自动化交互式仪表盘

    python交互式和文件式 In this tutorial, I will be creating an automated, interactive dashboard of Texas COVI ...

  5. python 网页编程_通过Python编程检索网页

    python 网页编程 The internet and the World Wide Web (WWW), is probably the most prominent source of info ...

  6. python 时间序列预测_使用Python进行动手时间序列预测

    python 时间序列预测 Time series analysis is the endeavor of extracting meaningful summary and statistical ...

  7. python 概率分布模型_使用python的概率模型进行公司估值

    python 概率分布模型 Note from Towards Data Science's editors: While we allow independent authors to publis ...

  8. python图像检测_用Python实现通过哈希算法检测图片重复的教程

    Iconfinder 是一个图标搜索引擎,为设计师.开发者和其他创意工作者提供精美图标,目前托管超过 34 万枚图标,是全球最大的付费图标库.用户也可以在 Iconfinder 的交易板块上传出售原创 ...

  9. python自动投递_利用python如何在前程无忧高效投递简历

    前言 在前程无忧上投递简历发现有竞争力分析,免费能看到匹配度评价和综合竞争力分数,可以做投递参考 计算方式 综合竞争力得分应该越高越好,匹配度评语也应该评价越高越好 抓取所有职位关键字搜索结果并获取综 ...

最新文章

  1. nginx+lua实现上传文件到OSS
  2. jbpm4 org.hibernate.exception.ConstraintViolationException
  3. [特征工程系列一] 论特征的重要性
  4. html与js与mysql_从本地html / javascript网站插入mySQL数据库
  5. Fliptile(状压+思维)
  6. Swift多线程:GCD进阶,单例、信号量、任务组
  7. android 遮罩 进度条,bootstrap创建带遮罩层的进度条
  8. echarts地图插入图片
  9. matlab2015使用dsolve错误,matlab - 当变量乘以常数时,dsolve中的错误(R2011a) - 堆栈内存溢出...
  10. Linux系统编程 40 -open函数
  11. z变换判断稳定性和因果性_信号与系统(奥本海姆)
  12. linux下zip文件密码破解Fcrackzip
  13. 如何提高文献检索能力?
  14. Jenkins 部署项目出现 Failed to connect to repository : Command git ls-remote -h http://gitlab. 128
  15. C++学习日记#2——幂法求矩阵的主特征值
  16. 线程中断标志位 interrupt()、interrupted()、isInterrupted() 的认识
  17. Hive常用库表操作及知识汇总
  18. 手机端宝贝描述中每张图片的宽要在480到1500之间,最大高度为2500, 以下图片不满足
  19. 高防CDN有什么特点?
  20. 4G EPS 中的 Bearer

热门文章

  1. python解决数学问题的实例_三十道python实战练习习题
  2. 解决塌陷的几种·办法
  3. windows上搭建php虚拟主机
  4. 又遇:只有偏执狂才能生存
  5. opencv检测乒乓球
  6. Spring rebooted --重新认识Spring
  7. Cucumber 1 -- 什么是BDD、Cucumber?它们的关系是什么?
  8. java 转义符 \\|
  9. POWER PIVOT的使用介绍1
  10. 【vSphere故障案例】案例十一:粉屏死机PSOD-3