Mary Dickson Diaz

Code, Life, Learning

week 2, what it do?

Hi everyone, Mary here, checking in for a Python – Week 2 update.

BUT FIRST: it is Friday at 3:30, I have a monster headache, and my ongoing lack of internet at home has made pretty much everything more difficult this week. However, you have come to this here website and by God, you shall have content!

My last class update was not super positive, I left feeling very frustrated with the class assignments and structure. Since then, I’m happy to report that week 2 was 10x better (if not 100x better); they did request feedback and definitely took it to heart — they added an additional TA (who I will be working with going forward) and made some other changes in response to the feedback. Including, we now have an opportunity to review the class material in advance, and come with questions and some python pre-work. So, yay.


oh how this headache rages… but the people demand code, and code they shall have.

Ok. So, here’s a useful thing: we are starting to get into python file functions, read and write. These are vital for using python with any type of file — in the case of playing with twitter bots I have used them with .txt files. I am so close (so so close) to fixing the bug in my bletchley bot wherein I have to manually update the text file every 24 hours or so. This code below successfully opens a file, prints a line, and then re-saves the file minus that line. See here:

# Question 2: can I open the file, print a line, and then re-write the 
# file with that line deleted? (I need this for a twitter bot...)

f ='bletchley.txt', 'rb')
file_data = f.readlines()
print file_data[0]
print numlines = len(file_data)
print type(file_data)

# this code above prints one line at a time... a good start. let's see 
# if we can now write everything else to a new file, 
# effectively deleting that line

outfile ='bletchley.txt', 'w')
for i in file_data[1:]:

# it works!

Ok, so that’s great. My python script can update the text file by itself. So now let’s just take that cool new function that I’ve built and stick it in the existing python script that’s running just fine:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import tweepy, time, sys
import os

argfile = str(sys.argv[1])

# enter the corresponding information from your Twitter application:
ACCESS_KEY = os.environ['ACCESS_KEY']
auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)


for line in f:
    time.sleep(1800) # Tweet every 30 minutes

I need the script to rewrite the text file after every tweet, so do you see how/where I would add the part about re-writing the text file into the loop? Yeah, me neither… because the open & close happens before the loop starts. So I think I need a bit of help with that, or just more time to mull on it / play with it. If you’ll recall, I did not write the latter script but rather adapted it from an open-source template, so I’m not sure what the whole argfile thing is. But, these questions certainly fall within the scope of the class.

No projects to show off yet (unless you want to admire my github repo), but hopefully soon. And, our lousy ISP says we should have internet at our home by April 1 (…). In the meantime, I have joined (and am blogging from) the fantastic Hillman City Collaboratory that I wrote about a little while ago.

Off to find advil and build bookshelves. Have a fantastic weekend!

1 Comment

  1. So, basically what the second script does is open a file, read all the lines into memory and tweet a line at a time every 30 minutes. You are right to be confused about where to enter the code for rewriting the text file; as it is currently written, you can’t. The loop happens after the file was already loaded into memory. In order to do what you’re attempting you’d need to re-write it to read the file from /within/ the loop.

Leave a Reply

© 2023 Mary Dickson Diaz

Theme by Anders NorenUp ↑