![](/img/trans.png)
[英]How to retrieve a filename and file created using a shell script that was called from a python script?
[英]File created by shell script is not opened by python?
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)
在調用read_file(logs)
之前,請確保:
logs = "log.txt"
如有必要,將完整路徑寫入“ log.txt”,例如:
logs = "/home/user/log.txt"
另外,而且我不確定這是否是您輸入的錯誤,請確保您這樣聲明read_file
:
def read_file(self, logs):
也就是說, self
必須首先。
您的logs
變量必須為字符串“ log.txt”才能正常工作。 顯然這是另外一回事。
如果文件內容有問題,則可能不是asccii,您可以導入編解碼器
def read_file(logs,self):打印'here'f1 = codecs.open(“ someFile”,“ r”,“ utf-8”)f1 = open(logs,“ r”,“ utf-8”)在f1中:打印str(line)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.