Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Python command line script
Index -> Programming, Python -> Python Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
QuantumPhysics




PostPosted: Wed Sep 05, 2012 5:56 pm   Post subject: Python command line script

I need help to write a simple command prompt (cmd) python program. the first line would be as follows

Python:


import os
raw_input("C:\>")



This program should run as a .exe and should run all system command (e.g. netstat, ipconfig/flushdns, color 0A-F) All those commands. The program will be saved as a .exe and will replicate the cmd when run, not exactly but just given input it will output all given prompts. I need help to what syntax to use for this. I need it by today. Please help.
Sponsor
Sponsor
Sponsor
sponsor
md




PostPosted: Thu Sep 06, 2012 9:35 am   Post subject: RE:Python command line script

Syntax is the wrong word methinks, as I suspect you are looking for an algorithm. I would get the input, parse it (likely by doing some string parsing) and then run other applications.

But my familiarity with python isn't high (haven't used it it months) so beyond that I couldn't tell you. If you're familiar with C++ and Java then try thinging about the problem in one of those languages and then figure out what the equivalent python would be.
mirhagk




PostPosted: Thu Sep 06, 2012 10:01 am   Post subject: RE:Python command line script

This kinda sounds like a school project.... it's only the first week though, tough teacher.
Zren




PostPosted: Thu Sep 06, 2012 1:33 pm   Post subject: RE:Python command line script

What does cmd.exe actually do?

I'll tell you right now that ipconfig is not bundled in cmd.exe (C:\Windows\System32\ipconfig.exe), as well as netstat. cmd.exe looks at the folders defined in the PATH variable set in the environment variables.

Look into the subprocess python module.
QuantumPhysics




PostPosted: Thu Sep 06, 2012 3:12 pm   Post subject: RE:Python command line script

Thanks alot guys. I'll try to follow the instructions you gave me and come back here with what I got after i'm done.

I just had to read up on the OS library in python, i have not really studied it thats why I asked.

This is sort of what I was looking to do same as this but in a command prompt manor


import os
// here get a text from the user and input it into a variable
os.system("system command here")

Run it like that but let it only prompt me to enter it once the console starts up. Then executes the system command and loops back to the get the system command. Just all im looking to do is re-write the cmd

much like this batch code:

@echo off
title Command Prompt - QuantumPhysics
ver
:cmd
set /p "cmd=%cd%>"
%cmd%
echo.
goto cmd

except in python... can I get any extra suggestions now that I explained my self for clearly? Or more elaboration on what you already stated? Thanks!

EDIT: This is not a project, i am just doing this for fun.
btiffin




PostPosted: Thu Sep 06, 2012 10:02 pm   Post subject: Re: Python command line script

Check the sources for IPython, https://github.com/ipython/ipython for safe methods at calling system programs.

And take a look at the code module, http://docs.python.org/library/code.html code.py will likely have quite a few hints on writing an interactive console, generally called REPL, Read Evaluate Print Loop, or shell.

Cheers
bl0ckeduser




PostPosted: Thu Sep 06, 2012 10:09 pm   Post subject: Re: Python command line script

I've tried to write a slightly improved-upon version, based on code [1] I found online.

code:

import os

while True:
        try:
                cmd = raw_input("> ")
                os.system(cmd)
        except EOFError:
                print ""
                break


os.system() will run the command in a new sub-shell [2] (i.e. new cmd.exe process on Windows),
which will do most of the real work, although I believe your program is still in charge
of the prompt string, environment variables, and path changes (i.e. the cd command --
it's a shell built-in on both Unix and Windows for technical reasons [3]); perhaps other things
(I'm no shell expert).

[1] http://ubuntuforums.org/showpost.php?p=6870746&postcount=4
[2] http://docs.python.org/library/os.html#os.system
[3] http://en.wikipedia.org/wiki/Cd_(command)#How_it_works
Display posts from previous:   
   Index -> Programming, Python -> Python Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: