pythontype-conversionpython-2.xspaces

Converting \x00 with a space


Im reading a /proc//cmdline file via:

pfile = '/proc/%s/cmdline' % pid
if os.path.isfile(pfile):
    fh = open(pfile, 'r')
    pname = repr(fh.read())
    fh.close()

print pname

OUTPUT:

'myexe\x00arg\x00arg2'

if i remove repr, there is no space between the words

'myexeargarg2'

So i changed it to

' '.join(fh.read().split('\x00'))

Then, I got:

'myexe arg arg2'

Just wondering if there are other ways to convert \x00 into a space?


Solution

  • Here a short example that will help you to answer your Question.

    content = b'cmd\x00arg1\x00arg2\x00'
    print(content.replace("\x00", " "))
    

    It will replace the occurences as explain here.