Search code examples
python-3.xfabric

How to fix print message issues in fabric


I am using Fabric3 1.12.post1 on Win10 X64. I tried the following program,

from fabric.operations import local
def platform_user():
   print('Platform: ' + local('echo %OS%'))
   print('Username: ' + local('echo %USERNAME%'))

the output is,

[localhost] local: echo %OS%
Windows_NT
Platform:
[localhost] local: echo %USERNAME%
my_user_name
Username:

Done.

'Platform' and 'Username' did not line up with the outputs, so how to fix this issue?


Solution

  • Use local with capture (http://docs.fabfile.org/en/1.12/api/core/operations.html):

    from fabric.operations import local
    def platform_user():
        result = local('echo %OS%', capture=True)
        print('Platform: ' + result.stdout)
        result = local('echo %USERNAME%', capture=True)
        print('Username: ' + result.stdout)