In [1]:
import subprocess as sp
In [25]:
sp.run(['ls', '-a'])
Out[25]:
In [26]:
p = sp.run(['ls', '-ap'], stdout=sp.PIPE)
In [27]:
print(p.stdout.decode())
In [32]:
p = sp.run(['ls', 'T___T'])
p.returncode
Out[32]:
In [34]:
p = sp.run(['ls', 'T___T'], check=True)
In [42]:
p = sp.run(['touch', '/dev/relationship'], stderr=sp.PIPE)
In [41]:
p.stderr
Out[41]:
In [43]:
'(%(eye)s%(nose)s%(eye)s)' % {'nose': '.', 'eye': '^'}
Out[43]:
In [44]:
b'(%(eye)s%(nose)s%(eye)s)' % {b'nose': b'.', b'eye': b'^'} # using %s in bytes is deprecated (= %b)
Out[44]:
In [ ]:
b'(%(eye)b%(nose)b%(eye)b)' % {b'nose': b'.', b'eye': b'^'}
In [ ]:
b'The unicode of kanji %a' % "漢字"
In [ ]:
b'The unicode of kanji %r' % "漢字" # using %r in bytes is deprecated (= %a)
In [ ]:
b'The unicode of kanji %b' % repr("漢字").encode('ascii', 'backslashreplace')
In [ ]:
b'The unicode of kanji %b' % "漢字".encode('utf8')
In [ ]:
b'The unicode of kanji \xe6\xbc\xa2\xe5\xad\x97'.decode('utf8')
In [45]:
b = 'Hello 你好'.encode()
b
Out[45]:
In [46]:
b.hex()
Out[46]:
In [47]:
bytes.fromhex(
# H e l l o
'48 65 6C 6C 6F 20'
# 你 好
'E4BDA0 E5A5BD'
).decode()
Out[47]:
In [48]:
b'({0}{1}{0})'.format('^', '.')
In [ ]: