Receiving TypeError while trying to write to file in Python

29,198

findall() as the name indicates returns a Python list and not a string/buffer.

You can not write a list to file handle - how should that work?

What do you expect?

file.write(str(tickers))

for the string representation of the list?

Or

file.write(', '.join(tickers))

for a comma-separated concatenation of the list items?

Anyway...write(..) requires a string or a buffer.

Apart from that: don't call your file handle 'file'.

file() is a build-in method.

Share:
29,198
Jared
Author by

Jared

Public Sector Consultant @ Deloitte

Updated on January 29, 2020

Comments

  • Jared
    Jared over 4 years

    When I run this code:

    tickers = re.findall(r'Process Name: (\w+)', s)
    
    file = open("C:\Documents and Settings\jppavan\My Documents\My Dropbox\Python Scripts\Processes\GoodProcesses.txt","w")
    file.write(tickers)
    file.close()
    

    It returns the common error:

    TypeError: expected a character buffer object

    Any ideas?