【发布时间】:2018-06-02 06:12:43
【问题描述】:
def _store_file(self, logVal):
if not os.path.exists('logs'):
print("[INFO] creating directory")
a = os.makedirs("logs")
print("INFO create Dir result: " + str(a))
f = None
try:
print("{INFO} Trying to open the file for writing logs")
f = open(self.conf['filename'], 'w')
print("{INFO} realpath of file is: " + os.path.realpath(f.name))
print("{INFO} abs path of file is: " + os.path.abspath(f.name))
res = f.write(str(logVal))
print("INFO file write: " + str(res))
except IOError as er:
print("[INFO]" + str(er))
finally:
f.close()
print("[INFO] WRITTING TO FILE")
print("sdfjsbdfkjbsdfkjbskjdfbskjfbskjbfkjsdbsdfksds",file=open('logs/temp.txt', 'w'))
我编写了一个中间件,它将请求数据发送到一个模块,该模块将日志存储在文件夹日志内的文件 debug.log 中。写入文件的代码没有给出任何错误,但我看不到任何名为 log 的文件夹或任何名为 debug.log 的文件。此外,当我尝试查看它提供的文件的完整路径时
/code/logs/debug.log
我已经搜索了整个系统,但找不到这条路径
上述代码的输出是:
web_1 | [EventTracker INFO]: log sent for storage
web_1 | [INFO] present working directory: .
web_1 | [INFO] creating directory
web_1 | INFO create Dir resutl: None
web_1 | {INFO} Trying to open the file for writing logs
web_1 | {INFO} realpath of file is: /code/debug.log
web_1 | {INFO} abs path of file is: /code/debug.log
web_1 | INFO file write: 432
web_1 | [INFO] WRITTING TO FILE
web_1 | [02/Jun/2018 06:39:16] "GET /community-view/1/ HTTP/1.1" 200 17353
web_1 | [EventTracker.sendRequestData INFO] new data object added to bucket
【问题讨论】:
-
您能否提供脚本的输出(即
{INFO}语句)? -
而不是使用
f = open('logs/' + self.conf['filename'], 'w'),使用os.path.join() -
@a874 我正在使用 docker 运行它,以便它可以在虚拟环境中运行。所以我认为问题是别的
-
使用绝对路径名而不是相对于当前目录的名称。
-
有点不相关,但是当 Python 的 stdlib 带有一个完全开发的日志包时,你到底为什么要重新发明方轮呢?