33

I am trying to count the number of lines in a file using Python functions. Within the current directory, while os.system("ls") finds the file, the command subprocess.Popen(["wc -l filename"], stdout=subprocess.PIPE) does not work.

Here is my code:

>>> import os
>>> import subprocess
>>> os.system("ls")
sorted_list.dat
0
>>> p = subprocess.Popen(["wc -l sorted_list.dat"], stdout=subprocess.PIPE)File "<stdin>", line 1, in <module>
File "/Users/a200/anaconda/lib/python2.7/subprocess.py", line 710, in __init__
    errread, errwrite)
File "/Users/a200/anaconda/lib/python2.7/subprocess.py", line 1335, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
user2105632
  • 751
  • 1
  • 7
  • 12

2 Answers2

60

You should pass the arguments as a list (recommended):

subprocess.Popen(["wc", "-l", "sorted_list.dat"], stdout=subprocess.PIPE)

Otherwise, you need to pass shell=True if you want to use the whole "wc -l sorted_list.dat" string as a command (not recommended, can be a security hazard).

subprocess.Popen("wc -l sorted_list.dat", shell=True, stdout=subprocess.PIPE)

Read more about shell=True security issues here.

In this case, to use the command as a whole string and avoid the security issues of shell=True you can use shlex

import subprocess
import shlex

command = "wc -l sorted_list.dat"
subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE)
Francisco Puga
  • 23,869
  • 5
  • 48
  • 64
bakkal
  • 54,350
  • 12
  • 131
  • 107
  • I am getting error : "python3: can't open file '$SDE/install/lib/python3.6/site-packages/p4testutils/bf_switchd_dev_status.py': [Errno 2] No such file or directory" even if shell=True. The same script runs properly on shell but gives error when called by subprocess. What may be the issue? – Nagmat Sep 15 '22 at 22:20
  • @Nagmat Is `$SDE` an env var or really the folder name? I think you may have to expand the value of `$SDE` in your python string first? – bakkal Sep 17 '22 at 01:45
  • it is an environmental variable. I resolved the issue by writing the long path. – Nagmat Sep 18 '22 at 22:35
6

The error occurs because you are trying to run a command named wc -l sorted_list.dat, that is, it is trying to find a file named like "/usr/bin/wc -l sorted dat".

Split your arguments:

["wc", "-l", "sorted_list.dat"]