Python日志记录模块没有保存到指定的文件名?

2024-09-27 00:20:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要分发一个用Python编写的应用程序,它将安装在用户计算机的Program Files文件夹中。由于程序文件默认为写保护,所以我需要将所有文件保存到Windows上的%APPDATA%文件夹中。我在做-我有设置.ini,设置_分贝以及在那里创建的另一个文件。在

但是,Python中的日志模块似乎拒绝将日志保存在那里,而是将其保存到根目录。这是我的代码:

currentPath = os.environ["APPDATA"] + "\\tools\\"

if not os.path.exists(currentPath):
    try:
        os.makedirs(os.environ["APPDATA"] + "\\tools\\")
        currentPath = os.environ["APPDATA"] + "\\tools\\"
    except:
        currentPath = os.getcwd()

if not os.path.exists(currentPath + "ubc.log"):
    open(currentPath + "ubc.log", "w").close()

print currentPath + "ubc.log"

#Let's define the logging "manager" as well as logging format...
logging.basicConfig(filename=currentPath + "ubc.log",
    format='%(asctime)-15s: %(name)-18s - %(levelname)-8s - %(module)-15s - %(funcName)-20s - %(lineno)-6d - %(message)s',
    level=logging.DEBUG)

logger = logging.getLogger("gui-log")

上面的print语句返回正确的结果,给我C:\Users\Bogdan\AppData\Roaming\utools\ubc.log。如果文件不存在,os.path.exists()部分将成功创建该文件。但是记录器不会记录在那里——而是记录到执行.py脚本的目录中的文件。在

不确定这是否重要,但是在我有filename="ubc.log",并且它确实记录到该文件之前,但是当需要分发这个应用程序时,我更改了它。在

有人有什么想法吗?在


Tags: 文件path文件夹log应用程序ifoslogging

热门问题