11

I'm trying the following and its failing with an error. I've tried to run it from Python shell/from a script/ on the windows console by invoking python on console. Nothing seems to work. Always the same error.

from subprocess import call
>>>pat = "d:\info2.txt"

>>> call(["type",pat])

>>>Traceback (most recent call last):
  File "<pyshell#56>", line 1, in <module>
    call(["type",pat])
  File "C:\Python27\lib\subprocess.py", line 493, in call
    return Popen(*popenargs, **kwargs).wait()
  File "C:\Python27\lib\subprocess.py", line 679, in __init__
    errread, errwrite)
  File "C:\Python27\lib\subprocess.py", line 893, in _execute_child
    startupinfo)
WindowsError: [Error 2] The system cannot find the file specified

does anyone know what is wrong here.!!???

even the simple call(["date"]] without any arguements also fails with the same error.

I'm using : Python 2.72 32bit version on a windows 7 machine.

LJNielsenDk
  • 1,268
  • 1
  • 15
  • 32
anotherCoder
  • 630
  • 3
  • 11
  • 24
  • 1
    what is `call` in your example? please include your imports? Assuming you mean the function from the `subprocess` package, I can't reproduce your error, since `call(["data"])` works perfectly fine on my system. What are your python version, os, and stuff like that? – David Zwicker Mar 02 '12 at 10:41
  • 1
    Here are the details you asked for : Yes, call is the function in subprocess. I've made the necessary edits now above. hope the situation is clear now. – anotherCoder Mar 02 '12 at 10:49
  • @David Zwicker: The DOS command `date` exists on Windows, it displays or sets the date. – BioGeek Mar 02 '12 at 12:01

4 Answers4

21

Add shell=True to call:

>>> import subprocess
>>> subprocess.call('dir', shell=True)
0

As you see, it gives as value the return code, not the output of dir. Also, it waits till the command completes, so doing

>>> subprocess.call('date', shell=True)

will wait for you to enter a new date.

edit: If you want to capture the output, use subprocess.check_output. The DOS command type for example prints out the contents of a file. So, suppose that your file info2.txt contains your username, you would do:

>>> import subprocess
>>> path = r'd:\info2.txt'
>>> output = subprocess.check_output(['type', path], shell=True)
>>> print output
Vinu

For all the ways to call external commands in Python, see this comprehensive overview to a related question, for more about subprocess specifically, see this article by Doug Hellmann.

Community
  • 1
  • 1
BioGeek
  • 19,132
  • 21
  • 75
  • 123
  • Thank you @BioGeek that was helpful. Its helps me solve the problem of the error, but doesn't help me with the main cause. All I wanted to do was **type a file out to the cmd console**. – anotherCoder Mar 02 '12 at 11:22
  • What do you mean by _type a file out to the cmd console_? If you want to use the DOS command `type` to capture the contents of a file, see my edit. If you want something else, please clarify. – BioGeek Mar 02 '12 at 11:37
  • NO, I've got the solution now. THank for your help @BioGeek – anotherCoder Mar 08 '12 at 11:43
  • 3
    +Vinu, so what was the solution? – Nakilon Apr 29 '13 at 15:56
  • @BioGeek while running in Python shell, subprocess.call('dir', shell=True) gives the output also along with return code.. am I missing something??? – DevC Mar 12 '14 at 11:43
  • @DevC [That's not what I'm seeing.](http://i.imgur.com/0tAAQiM.png). Could you post a screenshot of ` subprocess.call('dir', shell=True)` also giving the output of the `dir` command? – BioGeek Mar 12 '14 at 13:48
  • `shell=true` should be avoided according to [subprocess' manual](https://docs.python.org/2/library/subprocess.html#frequently-used-arguments). The correct solution should be @user1016274 's answer. – frank28_nfls Mar 31 '16 at 13:49
4

The 'type' command doesn't run because it's an internal command - internal to the command interpreter/shell called CMD.EXE. You have to call "cmd.exe type filename" instead. The exact code is:

call(['cmd','/C type abc.txt'])
user1016274
  • 3,501
  • 1
  • 18
  • 17
1
pat = "d:\info2.txt"

In Python and most other programming languages, \ is an escape character, which is not included in a string unless doubled. Either use a raw string or escape the escape character:

pat = "d:\\info2.txt"
sverre
  • 6,402
  • 2
  • 25
  • 35
  • Thanks for your reply, I tried that too. Its not working either. I would like to draw your attention to : **call(["date"])** is also failing with the same error. – anotherCoder Mar 02 '12 at 10:30
0

Escape the \ with \\:

pat = "d:\\info2.txt"

or use "raw" strings:

pat = r"d:\info2.txt"
Emil Ivanov
  • 35,757
  • 11
  • 70
  • 90
  • Thanks for your reply, I tried that too. Its not working either. I would like to draw your attention to : **call(["date"])** is also failing with the same error – anotherCoder Mar 02 '12 at 10:31