Saving tweets to different files using Python
<p>I am relatively new to python and trying to download tweets and save them to different text files. I want the file name to be dynamic and hence tried to modify code according to my requirement. Below, is the code that I am trying to modify:-</p>

<pre><code>class StdOutListener(StreamListener):
def on_data(self, data):   
    i=1
    try:
        if os.path.isfile('filename'+str(i)+'.txt'):
            if os.stat('filename'+str(i)+'.txt').st_size &gt; 5000000:
                i=i+1
    #           print data
                savefile=open('filename'+str(i)+'.txt','a')
                savefile.write(data)
                savefile.write('\n')
                savefile.close()
                return True
            else:
                savefile=open('filename'+str(i)+'.txt','a')
                savefile.write(data)
                savefile.write('\n')
                savefile.close()
                return True 
        else:
            savefile=open('filename'+str(i)+'.txt','a')
            savefile.write(data)
            savefile.write('\n')
            savefile.close()                
    except BaseException, e:
        print 'failed_ondata,',str(e)
        time.sleep(5)            
def on_error(self, status):
    print status
</code></pre>

<p>Something is off in the code above as it doesn't seem to work. I am still learning and It could be the most obvious thing but I would really appreciate if someone can help me work the code above. </p>

<p>A loop to increment i each time a file exist does not exist: your mechanism only works with file of index 1, it creates a file of index 2, then stops there.</p>

<p>Fix: I have added a loop which breaks as soon as a "free" filename is found. Check better way to get file size and code is much much more compact. Tested and works as designed: creates files, incrementing the number each time it is too big.</p>

<p>Here's my proposal:</p>

<pre><code>import os

class StdOutListener(StreamListener):
    def get_filename(self,i):
        return 'filename'+str(i)+'.txt'

    def on_data(self, data):
        i=1
        try:
            # compute first free file
            while True:
                f = self.get_filename(i)
                if os.path.isfile(f):
                    if os.path.getsize(f) &gt; 5000000:
                        i+=1  # next file index
                    else:
                        break  # file exists but size small enough
                else:
                    break  # ok file does not exist
            savefile=open(self.get_filename(i),'a')
            savefile.write(data)
            savefile.write('\n')
            savefile.close()
            return True  # done!
        except BaseException as e:
            print('failed_ondata,',str(e))
            time.sleep(5)
            return False

    def on_error(self, status):
        print(status)
</code></pre>

python python-2.7 twitter tweepy