Run Windows commands from Python console

Rick Johnson rantingrickjohnson at gmail.com
Sun Sep 3 10:00:01 EDT 2017


On Sunday, September 3, 2017 at 7:57:14 AM UTC-5, g.mor... at gmail.com wrote:
> Hello,
> 
> I run Python console in Windows. Can I run cmd prompt commands there?
> 
> If I run command dir I have:
> 
> >>> dir
> <built-in function dir>
>  
> What does it means?

It means that the expression `dir` (in python's universe) is
a symbol for the built-in function named "dir", and when you
send a symbol to the python interpreter in interactive mode,
all python will do is send back some info about the symbol.
In this case, Python is telling you that the symbol `dir` is
not only a function object, it is a _built-in_ function
object.

To actually _invoke_ a python function -- even when the
function requires no arguments (as is the case with "dir")
-- you must use enclosing parentheses. Try this instead:

    >>> dir()
    ['__builtins__', '__doc__', '__name__', '__package__']    
    
Some languages will allow you to omit the parentheses when
calling a function that requires no arguments, but python
does not work that way; and i'm glad too, because i have a
very low tolerance for inconsistency. Just ask anyone here
who knows me! O:-)

> If i trying to create file I have error:
> 
> >>> type NUL > hh.txt
>   File "<stdin>", line 1
>     type NUL > hh.txt
>            ^
> SyntaxError: invalid syntax

That's not how you create a file with Python! To read or
write files, try using the built-in function named "open":

    >>> fileObj = open("NEW_OR_EXISTING_FILENAME_HERE", 'w') # Write
    
or

    >>> fileObj = open("EXISTING_FILENAME_HERE", 'r') # Read

> What means line below:
> 
> File "<stdin>", line 1
> 
> I don't have any <stdin> file.

Sure you do, it's just not readily apparent. 

Python provides a "stdin" (aka: Standard Input) for which
input can be received, and a "stdout" (aka: Standard Output)
so that output can be, well, for lack of better word:
"outputted".

In fact, the built-in "print function" and built-in "input
function" are just wrappers around `sys.stdout.write(...)`
and `sys.stdin.readline('...')` respectively, and if you
don't believe me, then type this:

    >>> import sys
    >>> sys.stdout.write('Python rules!\n')
    Python rules!
    >>> value = sys.stdin.readline()
    this is input! # <-- Type this yourself!
    >>> value
    'this is input!\n'
    
The same result can be achieved using the more powerful and
elegant built-in functions:

    >>> value = input('Type Something Interesting: ')
    Type Something Interesting: blah
    >>> value
    'blah'
    >>> print('Python Rules!')
    Python Rules!
    
You can view these "file streams" by importing the "sys"
module and typing `sys.stdin` or `sys.stdout` at the command
line, and, depending on where you type these commands, you
may receive different results for the streams as they can be
captured and redefined by unique interactive environments.
Here is session from IDLE (Python's built-in code editor):

    >>> sys.stdin
    <idlelib.PyShell.PseudoInputFile object at 0x0267BB30>
    >>> sys.stdout
    <idlelib.PyShell.PseudoOutputFile object at 0x0267BB90>
    
As you can see, IDLE has captured these streams, so you'll
get a different result if you run the commands from a
windows prompt in python mode, or a unique environment.

You can even capture these streams for yourself, which is
helpful for logging purposes. Consult the "sys" module docs
for more details....





More information about the Python-list mailing list