Python读取Linux进程内存并转储到文件

标签 python memory python-2.7

我有以下脚本:

import sys, os

pid = sys.argv[1]
maps_file = open("/proc/%s/maps" % pid, 'r')
mem_file = open("/proc/%s/mem" % pid, 'r')
for line in maps_file.readlines():  # for each mapped region
    m = re.match(r'([0-9A-Fa-f]+)-([0-9A-Fa-f]+) ([-r])', line)
    if m.group(3) == 'r':  # if this is a readable region
        start = int(m.group(1), 16)
        end = int(m.group(2), 16)
        mem_file.seek(start)  # seek to region start
        chunk = mem_file.read(end - start)  # read region contents
        #print chunk,  # dump contents to standard output
        mem_dump = open(pid+".bin", "wb")
        mem_dump.write(str(chunk,))
        mem_dump.close()
maps_file.close()
mem_file.close()

到目前为止,一切正常(转储进程的内存),但我无法将数据保存到文件中。我做错了什么?

最佳答案

文件是否会被写入到您不希望的某个位置(看起来它们将被写入当前目录)?

关于Python读取Linux进程内存并转储到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16060237/

相关文章:

python - 从单词列表中创建句子 x 次

java - 增加 Eclipse 中的 JRE 内存使用量

python - Azure 部署未安装requirements.txt 中列出的Python 包

Python 无法导入 DataFrame

numpy - 步幅如何帮助遍历 numpy 中的数组?

c++ - 为结构体动态分配内存

Python 2.7 - 将一系列拆分为具有相同数量项目的间隔(与 pandas.cut() 类似)

python - 在 Django 表单中将所有 CharField 表单字段输入转换为小写

python - 未知错误 : Chrome failed to start: exited abnormally

python - 从多个拆分单个