1

echo "Denied" > log.txt

later log.txt is opened in python:

def read_file(logs,self): print 'here' f1=open(logs,"r","utf-8") for line in f1: print str(line) 

On running it gives error:

"TypeError: coercing to Unicode: need string or buffer, instance found" 
0

4 Answers 4

3

it probably should be:

def read_file(self, logs): 

with the reverse parameter order

def read_file(logs, self): 

your method try to open the self instance instead of logs when you call:

self.read_file(logs) 
Sign up to request clarification or add additional context in comments.

Comments

2

Before calling read_file(logs), make sure that:

logs = "log.txt" 

If necessary, write the complete path to "log.txt", for instance:

logs = "/home/user/log.txt" 

Also, and I'm not sure if this was a typo in your question, make sure that you declare read_file like this:

def read_file(self, logs): 

That is, self must come first.

Comments

1

Your logs variable would have to be the string "log.txt" for this to work. Apparently it is something else.

Comments

0

If the content of the file is the problem, maybe its not asccii you could do import codecs

def read_file(logs,self): print 'here' f1 = codecs.open( "someFile", "r", "utf-8" ) f1=open(logs,"r","utf-8") for line in f1: print str(line)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.