How to check if there is no output in python subprocess?
Question:
I use Python 3. This is a part of my script:
proc = subprocess.Popen(["git", "ls-remote", "--heads", "origin", "master"],stdout=subprocess.PIPE)
line = proc.stdout.readline()
if line != '':
print("not empty")
print(line)
else:
print(empty)
I want to check if there is a remote master. If the content of line
is not empty (= there is a master) it will print "not empty". If there is no master = no output I want to print empty.
But it does not work:
This is the output without python:
$ git ls-remote --heads origin xxx (no output)
$ git ls-remote --heads origin master (output)
a5dd03655381fcee94xx4e759ceba7aeb6456 refs/heads/master
This is the output when I run the script when master exists:
b'a5dd03655381fcee94xx4e759ceba7aeb6456trefs/heads/mastern'
And when master does not exist:
b''
How can I make this work? It seems the pipe seems to take different signs like b''
and n
and not only the words.
Answers:
You are getting a binary string as output; many programs do internal decoding and present you with the text string, but subprocess.Popen
is not one of them.
But you can easily decode it yourself:
out_bin = b'a5dd03655381fcee94xx4e759ceba7aeb6456trefs/heads/mastern'
out_txt = out.decode('utf-8') # Should be `utf-8` encoded
And the t
and n
are tab, and newline characters, that will do the respective formatting when you print
.
Moreover, all the methods of the text string, works on binary strings too, its just the implementation/presentation that differs.
Also, on your if line != '':
condition, you need to either explicitly tell that you are matching against a binary string by:
if line != b'':
Or better as empty strings are falsey in Python, simply do:
if not line:
I use Python 3. This is a part of my script:
proc = subprocess.Popen(["git", "ls-remote", "--heads", "origin", "master"],stdout=subprocess.PIPE)
line = proc.stdout.readline()
if line != '':
print("not empty")
print(line)
else:
print(empty)
I want to check if there is a remote master. If the content of line
is not empty (= there is a master) it will print "not empty". If there is no master = no output I want to print empty.
But it does not work:
This is the output without python:
$ git ls-remote --heads origin xxx (no output)
$ git ls-remote --heads origin master (output)
a5dd03655381fcee94xx4e759ceba7aeb6456 refs/heads/master
This is the output when I run the script when master exists:
b'a5dd03655381fcee94xx4e759ceba7aeb6456trefs/heads/mastern'
And when master does not exist:
b''
How can I make this work? It seems the pipe seems to take different signs like b''
and n
and not only the words.
You are getting a binary string as output; many programs do internal decoding and present you with the text string, but subprocess.Popen
is not one of them.
But you can easily decode it yourself:
out_bin = b'a5dd03655381fcee94xx4e759ceba7aeb6456trefs/heads/mastern'
out_txt = out.decode('utf-8') # Should be `utf-8` encoded
And the t
and n
are tab, and newline characters, that will do the respective formatting when you print
.
Moreover, all the methods of the text string, works on binary strings too, its just the implementation/presentation that differs.
Also, on your if line != '':
condition, you need to either explicitly tell that you are matching against a binary string by:
if line != b'':
Or better as empty strings are falsey in Python, simply do:
if not line: