0

I am executing below shell command in python to get the total number of line in a file and was wondering is there any way to assign the output to a variable in python? Thanks

import os
cmd = "wc -l" + " " + /path/to/file/text.txt
num_rows = os.system(cmd) 
Alex Man
  • 457
  • 4
  • 19
  • You might want to look into the [subprocess](https://docs.python.org/3/library/subprocess.html) module. – dspencer Mar 10 '20 at 16:27

1 Answers1

0

Try the os.exec class of functions in the os module. They are designed to execute the OS-dependent versions of the utils you're referring to. Shell scripts can also be executed in these processes.

https://docs.python.org/2/library/os.html#process-management

You may also want to try the subprocess module.

https://docs.python.org/2/library/subprocess.html

Azmat
  • 97
  • 1
  • 11