簡體   English   中英

python將信息級別記錄到文件

[英]python logging info level to a file

我開始使用日志記錄來跟蹤我的代碼中發生的事情,並從這里開始:

https://realpython.com/python-logging/

根據那篇文章,此代碼應創建並向名為 moses_data_extraction.log 的日志文件添加一行

import logging
import os

logging.basicConfig(filename='moses_data_extraction.log',
                    filemode='w',
                    format='%(asctime)s - %(message)s',
                    datefmt='%d-%b-%y %H:%M:%S',
                   level=logging.INFO)
logging.info('This will get logged to a file')
print(os.path.isfile('moses_data_extraction.log'))

最后一行給了我 FALSE

但是沒有創建文件。 實際上什么也沒發生,沒有文件沒有錯誤。

一些線索我做錯了什么?

如果最后一個打印語句為您提供True ,則必須在您當前的工作目錄中創建該文件。

要檢查文件所在的目錄,您可以使用以下命令檢查當前工作目錄:

import os
print(os.getcwd())

或者,您可以在代碼中指定完整的文件路徑名,以將日志文件放置在所需的位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM