【问题标题】:How to write PID to file in Python?如何在 Python 中将 PID 写入文件?
【发布时间】:2021-02-25 11:46:28
【问题描述】:

我通过os.getpid() 获取当前正在运行的脚本PID,并尝试将其存储在一个文件中,但我收到write() 只接受'string' 而不是'int' 的错误。那么如何将PID作为字符串传递给write()呢?

我的代码:

import os

outputFile = open('test.txt', "w")
pid = os.getpid()
outputFile.write(pid)
outputFile.close()

【问题讨论】:

标签: python crud pid


【解决方案1】:

我必须像这样使用类型转换器str()

import os

with open('test.txt', 'w', encoding='utf-8') as f:
    f.write(str(os.getpid()))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多