I tried to make a python script to execute "sudo cp -avr /home/pi/progs/ /var/www/" but wasn't able 'cause of errors. Now I want to simply execute "ls /var/www/" but I got these errors:

Traceback (most recent call last):
File "/var/www/copy.py", line 17, in <module>
File "/usr/lib/python2.7/subprocess.py", line 493, in call
  return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
  errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
  raise child_exception
OSError: [Errno 2] No such file or directory

My code is:

import distutils.core
import shutil
import os
#src = "/home/pi/Desktop/programy/www"
#dst = "/var/www"

import subprocess
#komenda = "ls /var/www"
glizda = os.path.split("/var/www/")
print glizda

2 = os.path.join("/var/www/")
print cipa
cipa2 = "ls "+cipa
print cipa2

tried many different actions. Anyone can help?

  • Do both the source and destination dirs exist? and if so what are the permissions? You can probably remove the os.path.join from line 13 and just set cipa = "/var/www/" have you tried this: import subprocess subprocess.call(["ls", "-l", "/var/www"]) or import os os.listdir("/var/www") Also, if you tell us what the big picture is rather than just the particular step you are having trouble with, we may be able to help more. Dec 7, 2014 at 22:15
  • +1 for polish var names :P subprocess.call() is used with a string array containing options, like this: subprocess.call(["ls", "myfolder"]) Dec 10, 2014 at 19:26

2 Answers 2


I use this sort of invocation.

#!/usr/bin/env python

import subprocess

result = subprocess.check_output(["ls", "/", "-l"])


result = subprocess.check_output(["/bin/ls", "/"])


It's probably best to give the full path to executables as in the second example.

The command and each argument should be separate elements in the list.

  • Thank you! My bad was subprocess.Popen("ls /var/www/") instead of subprocess.Popen(["ls","/var/www/"]) which works!
    – user22490
    Dec 8, 2014 at 21:09

If you don't need the output, try

os.system("ls -al /var/www")

EDIT: or the now recommended version (using shlex to make it easier to split the command):

import shlex
commandstring = "ls -al /var/www"
args= shlex.split(commandstring)

If you need the output, try the answer of joan. If you want to interact with the process while it is running, read the docs of subprocess.

  • subprocess.Popen("ls -al /var/www") Do not work.
    – user22490
    Dec 8, 2014 at 21:08
  • What is the error message? If there is no error message, everything should have worked. The subprocess.Popen does not give any output, so you don't actually see the output of the "ls" command, but the system has checked which files are there.
    – Michael
    Dec 8, 2014 at 21:25
  • Traceback (most recent call last): File "/var/www/copy.py", line 16, in <module> subprocess.Popen("ls -al /var/www") File "/usr/lib/python2.7/subprocess.py", line 679, in init errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory
    – user22490
    Dec 8, 2014 at 21:51
  • I've changed it to pass a list of arguments rather then a space seperated string of arguments. For simple commands like the one you are asking for, I normally still use os.system() because I can just pass the command and it just always works.
    – Michael
    Dec 9, 2014 at 10:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.