<

Saving programs in Python

Published on
9,881 Points
3,881 Views
Last Modified:
Approved
Here I am using Python IDLE(GUI) to write a simple program and save it, so that we can just execute it in future. Because when we write any program and exit from Python then program that we have written will be lost. So for not losing our program we save it.

So when we use Python IDLE(GUI) we can navigate to "File" and then click on "New Window" or we can use shortcut i.e."Ctrl+N" which will open a new text editor where we can write our program and save it into any location on the operating system. From the new window we can execute our program too.

Below are the screen shots taken after opening IDEL and then opening a New window.
Opening a new window
New window

Now let us write a simple program and save it on any location, but when we save the file the extension of the file name will always be ".py" :

x = raw_input("Enter your name : ")
print "Hello  " + x

Open in new window


Once it is saved on your system then navigate to "Run" and click on "Run Module" or "F5" which will run on the Python Shell as like below:

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
Enter your name : Sloba
Hello  Sloba
>>> 

Open in new window


I have named this program as "test.py". Below is the image of the file that I have saved it on my desktop.

Now we can also run this program by simply double click on it. But we will be unable to see the result after we enter the name as I have shown earlier, because it is processed so fast that we cannot see if the printed the input successfully or not.
So for avoiding this let us modify the program a bit and execute it from my desktop.

Modified code:
x = raw_input("Enter your name : ")
print "Hello  " + x
raw_input(" Hit <enter> to Exit ")

Open in new window


Now when we run the script from desktop then it will show the result after we pass the our name as input:

Enter your name : Sloba
Hello  Sloba
 Hit <enter> to Exit

Open in new window


So once we hit enter the command prompt will be closed. Now we come to know like how to save our Python program and if we want to hold before exit then we can make use of a function called "RAW_INPUT".
0
Author:Swadhin Ray
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free