Skip to main content
 首页 » 编程设计

python之shell脚本创建的文件不能被python打开

2025年05月04日16落叶

echo "Denied"> log.txt

稍后在python中打开log.txt:

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

运行时报错:

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

请您参考如下方法:

应该是:

def read_file(self, logs): 

参数顺序相反

def read_file(logs, self): 

您的方法尝试在您调用时打开 self 实例而不是日志:

self.read_file(logs)