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

Python读取Linux进程内存并转储到文件,python,memory,python-2.7,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])', l

我有以下脚本:

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()

到目前为止,所有的工作都很好(转储进程内存),但我无法将数据保存到文件中。我做错了什么?

可能是文件被写入了您不期望的地方(看起来它们将被写入当前目录)?

可能是文件被写入了您不期望的地方(看起来它们将被写入当前目录)?

发生了什么?有错误吗?没有错误,但根本没有写入文件:-发生了什么?你有错误吗?没有错误,但根本没有写入任何文件:-是的,你是对的,我希望写入当前目录,但没有,使用完整路径写入,谢谢!但仍然存在一个问题,该文件只使用一个块而不是所有数据写入,知道为什么吗?已解决:mem_dump=open(“/tmp/%s.bin”%pid,“ab”)@xtmtrx您也可以为
for
循环的完整
保持文件打开。是的,您是对的,我希望在当前目录中写入,但它们没有,它们已写入完整路径,谢谢!但仍然存在一个问题,该文件只使用一个块而不是所有数据写入,知道为什么吗?已解决:mem_dump=open(“/tmp/%s.bin”%pid,“ab”)@xtmtrx您还可以为整个
for
循环保持文件打开。