2016-11-08 20 views
-3
class F: 
    'test' 
    def __init__(self, line, name, file, writef): 
    self.line = line 
    self.name = name 
    self.file = file 

def scan(self): 
    with open("logfile.log") as search: 
     #ignore this part 
     for line in search: 
     line = line.rstrip(); # remove '\n' at end of line 
     if num == line: 
      self.writef = line 

def write(self): 
    #this is the part that is not working 
    self.file = open('out.txt', 'w'); 
    self.file.write('lines to note:'); 
    self.file.close; 
    print('hello world'); 

debug = F; 
debug.write 

それはエラーなしで実行しますが、何もしない、多くの方法を試してみましたが、オンラインで検索しかし、私はこの問題の唯一の一人です。なぜ、このシンプルなPythonのプログラムが動作しませんか?

+3

「F」と「書き込み」を忘れてしまった。単に 'F'と' debug.write'は本質的に何の操作もしません。 'self.file.close'も同様です。 –

+1

は...また – Julien

+4

あなたは(括弧付き) 'debug.writeを()'やりたいという意味、あなたのインスタンスのすべてのメソッドは、それは間違っていないのです場合は、同様にあなたのインデントを修正する必要があるかもしれません – jonrsharpe

答えて

2

インデントはPythonの構文の一部であるので、あなたはそれと一致しているのhabbitを開発する必要があります。 メソッドがクラスメソッドになるためには、インデントされている必要があります。

とにかく、ここには実行したスクリプトの修正版があります。

class F: 
    'test' 
    def __init__(self, line, name, file, writef): 
     self.line = line 
     self.name = name 
     self.file = file 
    def scan(self): 
     with open("logfile.log") as search: 
      #ignore this part 
      for line in search: 
       line = line.rstrip(); # remove '\n' at end of line 
       if num == line: 
        self.writef = line 
    def write(self): 
     # you should try and use 'with' to open files, as if you 
     # hit an error during this part of execution, it will 
     # still close the file 
     with open('out.txt', 'w') as file: 
      file.write('lines to note:'); 
     print('hello world'); 
# you also need to call the class constructor, not just reference 
# the class. (i've put dummy values in for the positional args) 
debug = F('aaa', 'foo', 'file', 'writef'); 
# same goes with the class method 
debug.write() 
関連する問題