Readlines() vs. Read() - Can I convert data back and forth between the two functions?
<p>I have a module file containing the following functions:</p>

<pre><code>def replace(filename):
    match = re.sub(r'[^\s^\w]risk', 'risk', filename)
    return match

def count_words(newstring):
    from collections import defaultdict
    word_dict=defaultdict(int)
    for line in newstring:
        words=line.lower().split()
        for word in words:
            word_dict[word]+=1

    for word in word_dict:
        if'risk'==word:
           return word, word_dict[word] 
</code></pre>

<p>when I do this in IDLE:</p>

<pre><code>&gt;&gt;&gt; mylist = open('C:\\Users\\ahn_133\\Desktop\\Python Project\\test10.txt').read()
&gt;&gt;&gt; newstrings=replace(mylist)    
&gt;&gt;&gt; newone=count_words(newstrings)
</code></pre>

<p>test10.txt just contains words for testing like:</p>

#

<p>risk risky riskier risk. risk?</p>

#

<p>I get the following error:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;pyshell#134&gt;", line 1, in &lt;module&gt;
    newPH = replace(newPassage)
  File "C:\Users\ahn_133\Desktop\Python Project\text_modules.py", line 56, in replace
    match = re.sub(r'[^\s^\w]risk', 'risk', filename)
  File "C:\Python27\lib\re.py", line 151, in sub
    return _compile(pattern, flags).sub(repl, string, count)
TypeError: expected string or buffer
</code></pre>

<p>Is there anyway to run both functions without saving <code>newstrings</code> into a file, opening it using <code>readlines()</code>, and then running <code>count_words</code> function?</p>
