Reputation: 3388
I have a price of code which transfers file from server to local machine. However, the directory the user enters may not be always correct. If the user enters the incorrect directory I am trying to show the error. If the returncode
is not None I will show the error. But this doesn't work. No matter what the program throws it always go to else
part.
#!/usr/bin/python
import subprocess
result = subprocess.Popen(['sshpass', '-p', 'password', 'rsync', '-avz', '--info=progress2', 'username@host_name:/file_name', '/home/zurelsoft/test'],
stdout=subprocess.PIPE)
if result.returncode != None:
print "Directory Incorrect"
else:
print "Done"
print result
Upvotes: 8
Views: 10544
Reputation: 43166
subprocess.Popen
returns an object (result variable in your case). You need to call the poll
or the wait
method of this object to set the returncode status. Then you can check the result.returncode
See http://docs.python.org/2/library/subprocess.html#subprocess.Popen.returncode
The child return code, set by poll() and wait() (and indirectly by communicate()). A None value indicates that the process hasn’t terminated yet.
Upvotes: 15