-1

I have a shell command(I'm running it on python) and I want to get its output and store it in a variable. Can you guys help me on how this can be done?

This is my python code...

os.system("unzip -p'" +filepath+"' docProps/app.xml | grep -oP '(?<=\<Pages\>).*(?=\</Pages\>)'"

By the way what it does is it gets the number of page/s of a docx file. It's working but as I've said I don't know how to store it in a variable.

When I do this:

page_number = os.system("unzip -p'" +filepath+"' docProps/app.xml | grep -oP '(?<=\<Pages\>).*(?=\</Pages\>)'"

It doesn't return the right value.

Thank you so much in advance. I tried converting this to a sub-process.call but I also don't know how because I'm a beginner. Thanks again!

Iron Fist
  • 10,739
  • 2
  • 18
  • 34
seluj rufo
  • 11
  • 3

2 Answers2

1

you can use subprocess to get the output

import subprocess
s=subprocess.Popen("unzip -p'{0}' docProps/app.xml | grep -oP '(?<=\<Pages\>).*(?=\</Pages\>)'".format(filepath),shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
for line in s.stdout.readlines():
  print line
s.kill()
Pavan Kumar T S
  • 1,539
  • 2
  • 17
  • 26
  • Hi thanks for replying! I tried your given code. It has no errors but it didn't output the number of pages. Thanks for your help anyways – seluj rufo Oct 13 '18 at 10:23
  • The subprocess returns whatever it gets from execution of shell code you wrote if its empty it means shell is not printing in console. Verify the shell command by printing it and executing on terminal. – Pavan Kumar T S Oct 13 '18 at 10:35
  • I found out why it returns nothing. When I ran the code in shell terminal it returns 1 but in python3 shell it returns 1 and then 0. Now how do I get the first output instead of the last? – seluj rufo Oct 13 '18 at 11:39
0

You can do this with the subprocess module in the Python Standard Library.

import subprocess

command = "unzip -p'" +filepath+"' docProps/app.xml | grep -oP '(?<=\<Pages\>).*(?=\</Pages\>)'"
completed_process = subprocess.run(command, shell=True, text=True, capture_output=True)
output = completed_process.stdout

Please note that if you use shell=True in subprocess.run() and the filepath input is not trusted (e.g. user-provided), you have a security problem, i.e., arbitrary code execution. The same goes for os.system(). See the official documentation for a possible solution using shlex.quote().

pietrodn
  • 561
  • 4
  • 9
  • Hi thank you for replying! I tried it but it gives me a TypeError: init () got an unexpected keyword argument 'capture output' – seluj rufo Oct 13 '18 at 10:18
  • However, when I removed text=True and capture_output=True. The errors were gone but I can get the value of output. – seluj rufo Oct 13 '18 at 10:19
  • Hey man! It just worked! Just have to change the subprocess.run() to subprocess.check_output(). THANKS again for your help ✋ – seluj rufo Oct 13 '18 at 14:05
  • Which Python version are you using? The subprocess API changed and this may be the cause of your problems :) – pietrodn Oct 14 '18 at 09:06
  • I'm using python 3.5.3 :) Thank you so much once again. This is a big help in our thesis, god bless man! – seluj rufo Oct 15 '18 at 08:18