0

我需要将用 Python 编写的应用程序作为 exe 分发,该应用程序将安装在用户计算机的 Program Files 文件夹中。由于 Program Files 默认是写保护的,所以我需要将所有文件保存到 Windows 上的 %APPDATA% 文件夹中。我正在这样做 - 我有 settings.ini、settings_db.db 和在那里创建的另一个文件。

但是,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", 并且它确实记录到该文件之前,但是当需要分发这个应用程序时,我改变了它。

有人有想法么?

4

0 回答 0