Getting TypeError when trying to write a file in Python

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 a general error:

TypeError: character buffer object expected

Any ideas?

+4
source share
1 answer

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

You cannot write a list to a file descriptor - how should this work?

What do you expect?

 file.write(str(tickers)) 

for a string representation of a list?

Or

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

for comma separated concatenation of list items?

In any case ... write (..) requires a string or buffer.

Also: do not name your file with the file descriptor.

file () is a built-in method.

+16
source

Source: https://habr.com/ru/post/1347939/


All Articles