0

I have a large text file that i want to be uri argument encoded. I researched a bit and came up with this:

import urllib
f=open('text.txt','r').read()
n= open('encodeTest.txt','w')
new=urllib.quote_plus(f) 
n.write(new)

I get this error whenever I run it:

AttributeError: 'module' object has no attribute 'quote_plus'
natalie
  • 99
  • 1
  • 3
  • 7

2 Answers2

1

You want urllib.parse.unquote_plus. But even this wouldn't work if you're encoding a large file since the max URL length is ~2000 chars

ganduG
  • 615
  • 6
  • 18
1

Python3 removed quote_plus from urllib's module. You need to import urllib.parse and go from there. However, there will be issues as you encode larger files so it probably makes sense to encode 1024 bytes at a time.

import urllib.parse
f=open('text.txt','r').read()
f.close()
n= open('encodeTest.txt','w')
for x in range(0, len(f), 1024):
    buf = f[x:x+1024]
    n.write(urllib.parse.quote_plus(buf))
n.close()
Ned Rockson
  • 1,095
  • 7
  • 16
  • What kind of issues are you talking about? I did end up encoding a large text file and it seemed to work fine, but since this is my first time doing this maybe I'm missing something? – natalie Jul 14 '15 at 01:05