我做了一個日誌功能,它有兩個參數:log_message
和mode
。出於某種原因,當我使用的功能和參數傳遞,我得到以下錯誤:函數不允許第二個參數
Traceback (most recent call last):
File "/Users/user/git/rip/rip.py", line 248, in <module>
main()
File "/Users/user/git/rip/rip.py", line 195, in main
log('STARTING RIPPER', 'i')
TypeError: log() takes 1 positional argument but 2 were given
這是奇怪的,因爲log()
絕對需要兩個參數。
這裏是我的代碼:
import os
import sys
import time
import mmap
import json
import requests
from bs4 import BeautifulSoup
from clint.textui import puts, colored
def log(log_message, mode='s'):
log_date = '[' + time.strftime("%d.%m_%H:%M:%S") + ']'
if mode == 'e':
log_file = 'test_error.log'
log_ouput = colored.white(log_date) + colored.red('[ERROR]' + log_message)
elif mode == 'i':
log_file = 'test_info.log'
log_ouput = colored.white(log_date) + colored.yellow('[INFO]' + log_message)
elif mode == 'c':
log_file = 'test_info.log'
log_ouput = colored.white(log_date) + colored.white('[COMMENT]' + log_message)
else:
log_file = 'test_download.log'
log_ouput = colored.white(log_date) + colored.green(log_message)
with open(log_file, 'a') as file_writer:
file_writer.write(log_message + '\n')
file_writer.close()
puts(log_ouput)
def main():
log('STARTING RIPPER', 'i')
能不能請你到什麼創建一個[Minimal,* Complete *和Verifiable示例](http://stackoverflow.com/help/mcve)並向我們展示? –
@Someprogrammerdude這個例子有什麼問題? – rhillhouse
上面的代碼看起來沒問題。爭議沒有問題。請讓我們知道你如何運行它。 – Raptor