Saving programs in Python

Swadhin Ray
CERTIFIED EXPERT
Published:
Updated:
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
5,005 Views
Swadhin Ray
CERTIFIED EXPERT

Comments (3)

Commented:
Nice article. One thing that jumped out at me was

 "Here I am using Python IDEL(GUI) to write a simple program and save it, so that we can just execute it in future."

Isn't it called Python IDLE?

-P0j0
CERTIFIED EXPERT

Author

Commented:
Thanks a lot  to catching this out :-)...  will change it right now ..

Regards,
Sloba
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
Since there is more than one IDE option for python, should the title of the article not be:  "Saving Python Programs in IDLE", or similar?

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.