Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8264513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:29:04+00:00 2026-06-08T04:29:04+00:00

I have a project in python that I am trying to run from IDLE

  • 0

I have a project in python that I am trying to run from IDLE using Python 2.7 When I run the program a text file does get created like I want, but is not getting any information written to it and I do not understand why this is happening. I am running it as a module by pressing the F5 key in IDLE on my Ubuntu 12.04 LTS laptop.

Here is the code:

import time
import MySQLdb
import tweepy
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream

# Go to http://dev.twitter.com and create an app.
# The consumer key and secret will be generated for you after
consumer_key=" # Omitted "
consumer_secret=" # Omitted "

# After the step above, you will be redirected to your app's page.
# Create an access token under the the "Your access token" section
access_token=" # Omitted "
access_token_secret=" # Omitted "

auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)

api = tweepy.API(auth)

# If the authentication was successful, you should
# see the name of the account print out
print api.me().name

class StdOutListener(StreamListener):
        """ A listener handles tweets are the received from the stream.
        This is a basic listener that just prints received tweets to stdout.

        """
        def on_data(self, data):
            print data
            return True

        def on_error(self, status):
            print status

if __name__ == '__main__':
        l = StdOutListener()

        stream = Stream(auth, l)    
        stream.filter(track=['#google'])

My file on github if anyone wants to work on it with me on github:
stocktwitterdb.py

A streaming example using tweepy can be found here on github:
tweepy streaming.py

Now that I have things coming across the shell I want to put them to a database or a text file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:29:06+00:00Added an answer on June 8, 2026 at 4:29 am

    There’s been some confusion in the changes you need to make, so I’m going to post a hopefully-fixed version here, along with some comments about why things are the way they are.

    import time
    import MySQLdb # also not currently used
    import tweepy
    from textwrap import TextWrapper # not used currently
    from getpass import getpass
    
    # define the format for messages here to avoid repetition
    OUT_STR = '''
    Status : %(text)s
    Author : %(author)s
    Date/Time : %(date)s
    Source : %(source)s
    Geo : %(geo)s
    
    
    -----------------------------------------------------------------------------
    
    
    '''
    
    class StockTweetListener(tweepy.StreamListener):
        def __init__(self, target):
            super(StockTweetListener, self).__init__();
            self.target = target
            # status_wrapper = TextWrapper(width=60, initial_indent=' ',
            #                             subsequent_indent=' ')
            # This isn't used in the current code. But, if you were going
            # to use it, you'd need to assign it to self.status_wrapper;
            # otherwise the variable would be local to this __init__ method
            # and inaccessible from anything else.
    
        def on_status(self, status):
            try:
                msg = OUT_STR % {
                    'text': status.text,
                    'author': status.author.screen_name,
                    'date': status.created_at,
                    'source': status.source,
                    'geo': status.geo,
                }
                print msg
                self.target.write(msg)
                # use self.target here. self is one of the paramaters to this
                # method and refers to the object; because you assigned to its
                # .target attribute before, you can use it here.
    
            except UnicodeDecodeError:
                # Catch any unicode errors while printing to console
                # and just ignore them to avoid breaking application.
                print "Record Skipped"
    
        def on_error(self, status_code):
            print 'An error has occured! Status code = %s' % status_code
            return True # keep stream alive
    
        def on_timeout(self):
            print 'Snoozing Zzzzzz'
    
    def main():
        username = raw_input('Twitter username: ')
        password = getpass('Twitter password: ')
        stock = raw_input('Name of Stocks(comma seperated): ')
        stock_list = [u for u in stock.split(',')]
        follow_list = None # ??? you don't seem to define this variable
    
        # open results.txt here and name it f locally. once code flow leaves
        # the with statement, in this case only through an exception happening
        # that jumps you out of the while loop, the file will be closed.
        with open('results.txt', 'w') as f:
             while True:
                 stream = tweepy.Stream(
                                username, password,
                                StockTweetListener(f), # passes the file to __init__
                                                       # as the "target" argument
                                timeout=None)
                 stream.filter(follow_list, stock_list)
    
    if __name__ == '__main__':
        try:
            main()
        except KeyboardInterrupt:
            quit()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have a python project that we want to start testing using buildbot. Its
We have a project that contains a library of Python and Scala packages, as
I need to have an at-home project now that I'm working on Python/Django at
I'm using SQLalchemy for a Python project, and I want to have a tidy
I have a Visual Studio project which uses nmake to call a Python file
I am trying to run a Django management command from cron. I am using
I'm writing a setup.py file for a Python project so that I can distribute
I'm trying to teach myself python using Google's AppEngine , and I can't get
For a python project I am trying an IDE, coming from vim but I
I am trying to write a python script that checks out a revision from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.