Basic python question
Hello, I am new to python and trying to do some basic things with python. I am writing a script that runs a df command and I need parts of that output saved in 2 different variables. Is this something that can be done? I can do this by running multiple df commands but would prefer to make only one call: -- inode_cmd = "/bin/df --output=pcent,ipcent /var| grep -v Use | tr '%' ' '" output = subprocess.check_output( inode_cmd, stderr=subprocess.STDOUT, shell=True ) -- But this would end up giving me the following: #df --output=pcent,ipcent /var | grep -v Use | tr '%' ' ' 5 1 I would like variable x to be 5 and variable y to be 1. Is there a easier way to do this? Thanks in advance for your guidance. -- https://mail.python.org/mailman/listinfo/python-list
Re: Basic python question
Thanks Aldwin that helps but it looks like it is reversing the numbers for some reason: the df command returns the following: 7 2 I used your example and did: x,y = set(output.split()) My assumption would be that x should be 7 and y should be 2. However, when I print x and y it seems to be reversed (x is 2 and y is 7). Am I missing something? Thanks On Wed, Oct 2, 2019 at 8:49 PM Aldwin Pollefeyt wrote: > > You could use: > > >>> x, y = set(output.split()) > > On Thu, Oct 3, 2019 at 11:44 AM Jagga Soorma wrote: >> >> Hello, >> >> I am new to python and trying to do some basic things with python. I >> am writing a script that runs a df command and I need parts of that >> output saved in 2 different variables. Is this something that can be >> done? I can do this by running multiple df commands but would prefer >> to make only one call: >> >> -- >> inode_cmd = "/bin/df --output=pcent,ipcent /var| grep -v Use | tr '%' ' '" >> output = subprocess.check_output( inode_cmd, >> stderr=subprocess.STDOUT, shell=True ) >> -- >> >> But this would end up giving me the following: >> >> #df --output=pcent,ipcent /var | grep -v Use | tr '%' ' ' >>5 1 >> >> I would like variable x to be 5 and variable y to be 1. Is there a >> easier way to do this? >> >> Thanks in advance for your guidance. >> -- >> https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: Basic python question
Thanks again Aldwin. This seems to work, guess it is the set that is flipping the numbers: x,y = (output.split()) Much appreciated! On Wed, Oct 2, 2019 at 9:19 PM Aldwin Pollefeyt wrote: > > Seems to work also: > > >>> [x,y] = output.split() > > On Thu, Oct 3, 2019 at 12:17 PM Aldwin Pollefeyt > wrote: >> >> Oh, sorry .. please try this: >> >> >>> x,y = tuple(output.split()) >> >> On Thu, Oct 3, 2019 at 12:11 PM Jagga Soorma wrote: >>> >>> Thanks Aldwin that helps but it looks like it is reversing the numbers >>> for some reason: >>> >>> the df command returns the following: >>> 7 2 >>> >>> I used your example and did: >>> x,y = set(output.split()) >>> >>> My assumption would be that x should be 7 and y should be 2. However, >>> when I print x and y it seems to be reversed (x is 2 and y is 7). Am >>> I missing something? >>> >>> Thanks >>> >>> On Wed, Oct 2, 2019 at 8:49 PM Aldwin Pollefeyt >>> wrote: >>> > >>> > You could use: >>> > >>> > >>> x, y = set(output.split()) >>> > >>> > On Thu, Oct 3, 2019 at 11:44 AM Jagga Soorma wrote: >>> >> >>> >> Hello, >>> >> >>> >> I am new to python and trying to do some basic things with python. I >>> >> am writing a script that runs a df command and I need parts of that >>> >> output saved in 2 different variables. Is this something that can be >>> >> done? I can do this by running multiple df commands but would prefer >>> >> to make only one call: >>> >> >>> >> -- >>> >> inode_cmd = "/bin/df --output=pcent,ipcent /var| grep -v Use | tr '%' ' >>> >> '" >>> >> output = subprocess.check_output( inode_cmd, >>> >> stderr=subprocess.STDOUT, shell=True ) >>> >> -- >>> >> >>> >> But this would end up giving me the following: >>> >> >>> >> #df --output=pcent,ipcent /var | grep -v Use | tr '%' ' ' >>> >>5 1 >>> >> >>> >> I would like variable x to be 5 and variable y to be 1. Is there a >>> >> easier way to do this? >>> >> >>> >> Thanks in advance for your guidance. >>> >> -- >>> >> https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Print formatting
Hello, I am new to python and trying to write a script that outputs some data about users. I was able to write it and dump the data but can't seem to align the output in column 2 correctly. Here is what I am trying to do: -- output: user1 data1 username2 data2 user3 data3 snip from script: print(str(temp_list[0]) + "\t\t" + str(temp_list[1])) -- Adding the tabs does not seem to work and I am sure there is a better way to do this. Any help would be appreciated. Thanks, -J -- https://mail.python.org/mailman/listinfo/python-list
Re: Print formatting
I seem to have found a way to do this with the following: print('{:<12s}{:>12s}'.format((temp_list[0]),(temp_list[3]))) Still let me know if there is a better way to format this output :) Thanks, -J On Fri, Oct 18, 2019 at 10:03 AM Jagga Soorma wrote: > > Hello, > > I am new to python and trying to write a script that outputs some data > about users. I was able to write it and dump the data but can't seem > to align the output in column 2 correctly. Here is what I am trying > to do: > > -- > output: > user1 data1 > username2 data2 > user3 data3 > > snip from script: > print(str(temp_list[0]) + "\t\t" + str(temp_list[1])) > -- > > Adding the tabs does not seem to work and I am sure there is a better > way to do this. Any help would be appreciated. > > Thanks, > -J -- https://mail.python.org/mailman/listinfo/python-list
python2 vs python3
Hello, I am writing my second python script and got it to work using python2.x. However, realized that I should be using python3 and it seems to fail with the following message: -- Traceback (most recent call last): File "test_script.py", line 29, in test_cmd = ("diskcmd -u " + x + " | grep -v '\*' | awk '{print $1, $3, $4, $9, $10}'" ) TypeError: Can't convert 'bytes' object to str implicitly -- I then run this command and save the output like this: -- test_info = (subprocess.check_output( test_cmd, stderr=subprocess.STDOUT, shell=True )).splitlines() -- Looks like the command output is in bytes and I can't simply wrap that around str(). Thanks in advance for your help with this. -- https://mail.python.org/mailman/listinfo/python-list