How to save python codes in files?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • why?

    #1

    How to save python codes in files?

    Im working with Python 2.2 on my red hat linux system. Is there any
    way to write python codes in separate files and save them so that i
    can view/edit them in the future? Actually I've just started with
    python and would be grateful for a response. Thanx!

  • Evan Klitzke

    #2
    Re: How to save python codes in files?

    On 6/12/07, why? <jimbomaan@gmai l.comwrote:
    Im working with Python 2.2 on my red hat linux system. Is there any
    way to write python codes in separate files and save them so that i
    can view/edit them in the future? Actually I've just started with
    python and would be grateful for a response. Thanx!
    Of course -- just put the code into a text file (using your favorite
    text editor) and then run the script using the python command, e.g. by
    executing on a command line:
    python my_program.py

    Since you're on a Linux system you can also use this as the first line
    of your file and then chmod +x the file to make it into an executable
    script:

    #!/usr/bin/env python

    --
    Evan Klitzke <evan@yelp.co m>

    Comment

    • why?

      #3
      Re: How to save python codes in files?


      Evan Klitzke wrote:
      On 6/12/07, why? <jimbomaan@gmai l.comwrote:
      Im working with Python 2.2 on my red hat linux system. Is there any
      way to write python codes in separate files and save them so that i
      can view/edit them in the future? Actually I've just started with
      python and would be grateful for a response. Thanx!
      >
      Of course -- just put the code into a text file (using your favorite
      text editor) and then run the script using the python command, e.g. by
      executing on a command line:
      python my_program.py
      >
      Since you're on a Linux system you can also use this as the first line
      of your file and then chmod +x the file to make it into an executable
      script:
      >
      #!/usr/bin/env python
      >
      --
      Im still unable to follow... :(
      See, if i have to save the following code in a file, how should i
      proceed?
      >>def sum(x,y): return x+y
      ....
      >>>

      Comment

      • Evan Klitzke

        #4
        Re: How to save python codes in files?

        On 6/13/07, why? <jimbomaan@gmai l.comwrote:
        >
        Evan Klitzke wrote:
        On 6/12/07, why? <jimbomaan@gmai l.comwrote:
        Im working with Python 2.2 on my red hat linux system. Is there any
        way to write python codes in separate files and save them so that i
        can view/edit them in the future? Actually I've just started with
        python and would be grateful for a response. Thanx!
        Of course -- just put the code into a text file (using your favorite
        text editor) and then run the script using the python command, e.g. by
        executing on a command line:
        python my_program.py

        Since you're on a Linux system you can also use this as the first line
        of your file and then chmod +x the file to make it into an executable
        script:

        #!/usr/bin/env python

        --
        Im still unable to follow... :(
        See, if i have to save the following code in a file, how should i
        proceed?
        >def sum(x,y): return x+y
        ...
        >>
        If you want to write a program, you need to have some code that
        actually does something (rather than just containing definitions of
        functions). Here's a trivial example of a file using your sum
        function:

        #!/usr/bin/env python

        def sum(x, y):
        return x+y

        print 'Enter two numbers:'
        x = int(raw_input() )
        y = int(raw_input() )

        print 'The sum is equal to %s + %s = %s' % (x, y, sum(x,y))

        --
        Evan Klitzke <evan@yelp.co m>

        Comment

        • markacy

          #5
          Re: How to save python codes in files?

          On 13 Cze, 10:06, why? <jimbom...@gmai l.comwrote:
          Evan Klitzke wrote:
          On 6/12/07, why? <jimbom...@gmai l.comwrote:
          Im working with Python 2.2 on my red hat linux system. Is there any
          way to write python codes in separate files and save them so that i
          can view/edit them in the future? Actually I've just started with
          python and would be grateful for a response. Thanx!
          >
          Of course -- just put the code into a text file (using your favorite
          text editor) and then run the script using the python command, e.g. by
          executing on a command line:
          python my_program.py
          >
          Since you're on a Linux system you can also use this as the first line
          of your file and then chmod +x the file to make it into an executable
          script:
          >
          #!/usr/bin/env python
          >
          --
          >
          Im still unable to follow... :(
          See, if i have to save the following code in a file, how should i
          proceed?
          >
          >def sum(x,y): return x+y
          ...
          :D You should try some basic manual on linux :D

          Copy and paste the code (do not include >>and ...) to the editor,
          save. Then run as advised above.
          Good luck.

          Cheers,
          Marek

          Comment

          • why?

            #6
            Re: How to save python codes in files?

            I tried but its not working. Here's a code for sum of two numbers. Now
            how do i save it?
            >> #! /usr/bin/env python
            ....
            >>def sum(x,y):
            .... return x+y
            ....
            >>x=int(raw_inp ut('Enter a number: '))
            Enter a number: 35
            >>y=int(raw_inp ut('Enter a number: '))
            Enter a number: 7
            >>print 'sum is', sum(x,y)
            sum is 42

            Comment

            • why?

              #7
              Re: How to save python codes in files?

              Also, how can i save a file using text editor in linux?

              Comment

              • Gabriel Genellina

                #8
                Re: How to save python codes in files?

                En Thu, 14 Jun 2007 01:56:13 -0300, why? <jimbomaan@gmai l.comescribió:
                I tried but its not working. Here's a code for sum of two numbers. Now
                how do i save it?
                >
                >>> #! /usr/bin/env python
                ...
                >>>def sum(x,y):
                ... return x+y
                ...
                >>>x=int(raw_in put('Enter a number: '))
                Enter a number: 35
                >>>y=int(raw_in put('Enter a number: '))
                Enter a number: 7
                >>>print 'sum is', sum(x,y)
                sum is 42

                You *don't* write your program inside Python; the interactive prompt is
                just for testing a few lines of code. Instead, use another program (a text
                editor) to create a file containing your source code, save it using a name
                like test1.py, and run it from a shell prompt using: python test1.py

                I think that Red Hat comes with gedit as the default editor, you should
                find it under Accesories, Text Editor or similar; any other editor (like
                nano) would be fine too. Open it and write your program:

                === begin ===
                #! /usr/bin/env python

                def sum(x,y):
                return x+y

                x=int(raw_input ('Enter a number: '))
                y=int(raw_input ('Enter a number: '))
                print 'sum is', sum(x,y)
                === end ===

                Copy all text between the marks === begin ===, and === end === (but not
                including those marks).
                Save it into your home directory - when prompted for a name, type
                "test1.py" (without quotes). (If you don't know what is your home
                directory, read your OS documentation).
                Now open a shell prompt (or terminal). Run the command "ls" (without
                quotes; press ENTER). You should see test1.py listed. (If not, use the cd
                command to go to your home directory and try again). Now execute "python
                test1.py" (again, without quotes) and you should be asked for the numbers,
                and the program should display the sum (as you already have seen inside
                the Python interpreter).
                If you want to change the program: go back into the editor, modify it,
                save it (with the same name, or a different one), switch to the shell
                prompt and run it again.
                These are the very basics of writing a text file and executing a Python
                program from the shell prompt. There are more powerful editors that are
                aware of Python syntax, by example, and can display the source code with
                different colors for keywords, numbers, comments, etc. Other programs
                combine an editor + debugger + code autocompletion + other nice features
                (they're called IDEs, in general).

                I hope this is of some help. You should read the OS documentation for more
                info on how to edit a file and such things.

                --
                Gabriel Genellina

                Comment

                • BartlebyScrivener

                  #9
                  Re: How to save python codes in files?

                  On Jun 13, 12:04 am, why? <jimbom...@gmai l.comwrote:
                  Im working with Python 2.2 on my red hat linux system. Is there any
                  way to write python codes in separate files and save them so that i
                  can view/edit them in the future? Actually I've just started with
                  python and would be grateful for a response. Thanx!
                  In addition to the help you've already received, you need:



                  It doesn't seem to be loading at the moment, but it will soon, I
                  suspect.

                  Otherwise, go to

                  http://tinyurl.com/w7wgp

                  Skip the installation instructions (for Windows) and go to the four
                  references and links under the image of the PythonWin interpreter.

                  HTH

                  rd

                  Comment

                  Working...