How to save python codes in files?

W

why?

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!
 
E

Evan Klitzke

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
 
W

why?

Evan said:
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?
 
E

Evan Klitzke

Im still unable to follow... :(
See, if i have to save the following code in a file, how should i
proceed?

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))
 
M

markacy

Im still unable to follow... :(
See, if i have to save the following code in a file, how should i
proceed?

...

: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
 
W

why?

I tried but its not working. Here's a code for sum of two numbers. Now
how do i save it?
.... return x+y
....sum is 42
 
G

Gabriel Genellina

I tried but its not working. Here's a code for sum of two numbers. Now
how do i save it?

... return 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.
 
B

BartlebyScrivener

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:

http://wiki.python.org/moin/BeginnersGuide

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top