Showing posts from February, 2010

Using Python command line

Recently I had a task to write a command file for Windows that run a simple Python commands via python –c command. The manual says that command may contain multiple statements separated by newlines. Unfortunately, Windows console doesn’t allow it (and Ctrl-T trick doesn’t help) so I had to find a workaround. The intuition and language reference helped me to find out that the simple statements can be separated by semicolons like in C/C++ (however, these languages allow to separate all the commands, not
the only simple ones).

Let me give an example. Imagine that accidentally I want to know the error message corresponding to the error code 9. Of course, I can read manuals and documentation, but there is a more simple way:

$ python -c "import os;print os.strerror(9)" Bad file descriptor
But this "trick" works only with simple statements. It is not allowed to mix simple and compound statements, so the following command will not work: python -c "import os;for i in …