bash - File Writing and Redirect output not working when using URLLIB2 in python -
bash - File Writing and Redirect output not working when using URLLIB2 in python -
using python urllib2 module create calls web page , result. script runs fine when don't redirect output of script file like:
class="lang-py prettyprint-override">python make_req.py #or python make_req.py & however when trying redirect output of command file like:
class="lang-py prettyprint-override">python make_req.py > file_req.out & in case output file empty after killing process process continues run.
i tried using file pointer within file writing output doesn't work when running code.
i have tried commenting out line data = urllib2.urlopen(full_url) in , has solved problem. when uncomment line problem reappears. believe because of urllib2 module. without file redirect or write file pointer, info line works perfect.
please suggest alternative or solution.
my code pretty simple follows:
class="lang-py prettyprint-override"># create request import urllib2 import urllib def makereq(name, fp1): info = {} data['name'] = name url_values = urllib.urlencode(data) #print url_values # order may differ. url = 'some_url' full_url = url + '?' + url_values print >> fp1, full_url info = urllib2.urlopen(full_url) open("names.tsv") fp , open("file_req.out", "wb+") fp1: uniq_names = fp.read().splitlines() name in uniq_names: name = name.split('\t') if int(name[1]) > 10: makereq(name[0], fp1) print >> fp1, "finished processing %s count %s" % (name[0], name[1]) print >> fp1, "finished processing names."
it appears open file file_req.out , write results in within program, later redirect stdout same file. later content override former info , later 1 nothing, see file_req.out empty.
python bash httprequest urllib2
Comments
Post a Comment