您好,当我用python编写以下代码时
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler('ThingsSpeakRESTAPI.log')
fh.setLevel(logging.DEBUG)将以ThingsSpeakRESTAPI.log名称创建日志文件
但如果我这么做了:
filename = datetime.datetime.now().isoformat()+"LOG.log"
print(filename)
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler(filename)
fh.setLevel(logging.DEBUG)然后我得到了下面的错误
OSError: [Errno 22] Invalid argument两者都在str中,那么为什么不带字符串值的变量,而是字符串值本身呢??
发布于 2021-11-01 13:09:38
文件名不能包含:字符。
你可以这样做:
filename = datetime.datetime.now().strftime("%Y-%m-%d_%H.%M.%S.%f")+"LOG.log"
在这里,您可以自己格式化字符串,这样可以确保文件名不包含非法字符。
结果:
2021-11-01_14.10.46.091188LOG.log
https://stackoverflow.com/questions/69796960
复制相似问题