41

When trying to apply some code I found on the internet in iPython, it's coming up with an error:

TypeError                                 Traceback (most recent call last)
    <ipython-input-4-36ec95de9a5d> in <module>()
     13     all[i] = r.json()
     14 
---> 15 cPickle.dump(all, outfile)

TypeError: argument must have 'write' attribute

Here's what I have done in order:

outfile = "C:\John\Footy Bants\R COMPLAEX MATHS"

Then, I pasted in the following code:

import requests, cPickle, shutil, time

all = {}
errorout = open("errors.log", "w")

for i in range(600):
    playerurl = "http://fantasy.premierleague.com/web/api/elements/%s/"
    r = requests.get(playerurl % i)

    # skip non-existent players
    if r.status_code != 200: continue

    all[i] = r.json()

cPickle.dump(all, outfile)

Here's the original article to give you an idea of what I'm trying to achieve:

http://billmill.org/fantasypl/

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Johnliquid
  • 413
  • 2
  • 5
  • 6

1 Answers1

69

The second argument to cPickle.dump() must be a file object. You passed in a string containing a filename instead.

You need to use the open() function to open a file object for that filename, then pass the file object to cPickle:

with open(outfile, 'wb') as pickle_file:
    cPickle.dump(all, pickle_file)

See the Reading and Writing Files section of the Python tutorial, including why using with when opening a file is a good idea (it'll be closed for you automatically).

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks Martijn, There is now file though, I'm trying to download some data to the outfile, or atleast that's what I think I'm trying to do! This is waht i'm trying to replicate: http://billmill.org/fantasypl/ – Johnliquid Mar 18 '15 at 16:44
  • @Johnliquid: it's impossible for me to see where you are missing indentation here; the block under the `with` statement does have to be indented just like with an `if` or `while` or `for` statement. – Martijn Pieters Mar 18 '15 at 16:47
  • File "", line 7 playerurl = "http://fantasy.premierleague.com/web/api/elements/%s/" ^ IndentationError: expected an indented block – Johnliquid Mar 18 '15 at 16:53
  • The upward arrow is just beneath playerurl – Johnliquid Mar 18 '15 at 16:53
  • @Johnliquid: so, did you indent that line just like in the blog post? Python requires the use of leading whitespace to denote blocks. Perhaps you need to start with the Python tutorial instead? – Martijn Pieters Mar 18 '15 at 16:55
  • I have now fixed the identation, thanks for this. I now have this error: IOError: [Errno 13] Permission denied: 'c:\etc.' I have permission to write to the folder and it's not open anywhere. The folder exists but the file i want to create doesn't – Johnliquid Mar 18 '15 at 17:13
  • @Johnliquid: make sure you don't have escape sequences in your filename; see [Python 3.4.1 script syntax error, arcpy &](https://stackoverflow.com/a/28706216) for some details. – Martijn Pieters Mar 18 '15 at 17:26
  • Thanks Martijn, followed the advice, removed escape sequences in the file name, still get this error: --------------------------------------------------------------------------- IOError Traceback (most recent call last) in () 13 all[i] = r.json() 14 ---> 15 with open(outfile, 'wb') as pickle_file: 16 cPickle.dump(all, pickle_file) IOError: [Errno 13] Permission denied: 'C:/John/Footy Bants/R COMPLAEX MATHS' – Johnliquid Mar 18 '15 at 19:40
  • @Johnliquid: Does the `C:\John\Footy Bants\` folder exist? Do you have write access to it? – Martijn Pieters Mar 18 '15 at 21:09
  • Hi Martijn, yes it does exist and I have also checked write permissions and have given myself full control. This hasn't changed the error. Could someone maybe try it themselves? I'm stumped! – Johnliquid Mar 19 '15 at 09:10
  • @Johnliquid: It is hard to say why it doesn't work for you. Try using `open()` in an interactive Python interpreter; use `'w'` or `'wb'` as the mode, and try it in different directories. Try and figure out why it might be failing in just that folder, or if it is failing in others too. – Martijn Pieters Mar 19 '15 at 09:32