2017-01-10 56 views
0
def read_contents(self,filename): 
    with open(filename,'r') as f: 
     lines=f.read().splitlines() 
     print lines 

s=read_contents('input.txt') 

當試圖運行該程序的錯誤拋出需要兩個參數和自我需要是存在的論據之一(這是一個更大的代碼的一部分) 如何做我通過文件名作爲參數沒有得到錯誤我如何傳遞參數自

回答

1

你必須在一個類中使用它:

class Test: 
    def read_contents(self, filename): 
     with open(filename, 'r') as f: 
      lines = f.read().splitlines() 
      print lines 

test = Test() 
s = test.read_contents('input.txt') 

或刪除自:

def read_contents(filename): 
    with open(filename, 'r') as f: 
     lines = f.read().splitlines() 
     print lines 

s = read_contents('input.txt')