1

そのため、1 日より新しいファイルをチェックするスクリプトがあります。今日作成されたように、このディレクトリに 1 日以内に作成されたファイルがない場合、スクリプトから電子メールが送信されるようにします。

私のスクリプトをお見せしましょう:

new_files = [] #list of files newer than 1 day
for f in os.listdir(path):
 fn = os.path.join(path,f)
 ctime = os.stat(fn).st_ctime
 if ctime > now - 1 * 86400:
 #this is a new file
  new_files.append(fn)
 if new_files(): #checks the list
  sendmail #calls sendmail script that sends email

それで、if new_files():リストに何かが追加されているかどうかを確認するためにリストをチェックしていますか?そうでない場合は、プロセスが失敗したため、発券システムに警告メールを生成して知る必要があります。これが私の問題です。リストの確認方法がわかりません。スクリプトを実行すると、TypeError: 'list' object is not callable.

これを行う正しい方法は何ですか?

4

2 に答える 2

1

PEP 8の引用

シーケンス(文字列、リスト、タプル)の場合、空のシーケンスはfalseであるという事実を使用します。

Yes: if not seq:
     if seq:

No: if len(seq)
    if not len(seq)

あなたのコードの場合:

if new_files:
于 2012-10-02T21:04:38.050 に答える
0

発生回数を数えることは役に立ちます。

new_files = [] #list of files newer than 1 day
for f in os.listdir(path):
   fn = os.path.join(path,f)
   ctime = os.stat(fn).st_ctime
   if ctime > now - 1 * 86400:
        countit=new_files.count(fn)  #count previous occurence
        #this is a new file
        new_files.append(fn)
        if new_files.count(fn)>countit: 
             sendmail #calls sendmail script that sends email
于 2012-10-02T21:03:12.203 に答える