How do I save Python scripts in Linux via the terminal?

March 20, 2014
Pinterest Stumbleupon Whatsapp

So I recently installed Ubuntu Linux onto my computer, and I am wondering how to save Python programs in the Terminal. I looked online for this one video, but it didn’t work.

Any and all help is much appreciated.

  1. James B
    March 21, 2014 at 5:30 pm

    It sounds like you're writing things line by line into the python "shell", or interpreter. Instead of doing that, create your programs as text files in any text editor (or the command line using nano), save them as whatever.py, then run them using python.

    Skip down to "running a sample program" (only it would be your program, not the sample):

    http://anh.cs.luc.edu/python/hands-on/3.1/handsonHtml/idle.html#running-a-sample-program

  2. Carl Swanson
    March 20, 2014 at 9:47 pm

    Vi or nano ought to do the trick

    • Cristián T
      March 21, 2014 at 1:54 am

      Sorry, I'm relatively new to Python, so please bear with me.

      Anyways, once I've written all the code that I wanted to write, do I just type "vi" or "nano" and then the file is saved? Also, how would I set the directory to save it in?

    • Bruce E
      March 21, 2014 at 6:39 am

      Vi and nano are character-based text editors that run in the terminal. You type 'vi' or 'nano' to start the editor and write your code. In vi, you use ':w' to write the file to disk. But your question suggests you already wrote the code, so what did you use to write it? Are you using an IDE for this or what?

    • Cristián T
      March 21, 2014 at 5:27 pm

      Well I'm not exactly sure what I am using. I followed a tutorial on how to install Python on Ubuntu (https://www.youtube.com/watch?v=MH4anq35I4o). After I was done with the installation, I learned that the way to open Python, in this case, is to open the Terminal and type "python" and press enter. After I am done with that, I am able to write the code I want as if I were just using a regular IDE. The only problem is that, as far as most IDEs go, there is usually an option to save the script. However, in this scenario, there is none, since I am writing everything through the Terminal. I was wondering how to save the scripts I wrote in the .py file format, as well as to specify the directory in which the file will be saved.