By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,281 Members | 2,201 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,281 IT Pros & Developers. It's quick & easy.

How to save python codes in files?

P: n/a
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!

Jun 13 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 6/12/07, why? <ji*******@gmail.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 <ev**@yelp.com>
Jun 13 '07 #2

P: n/a

Evan Klitzke wrote:
On 6/12/07, why? <ji*******@gmail.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
....
>>>
Jun 13 '07 #3

P: n/a
On 6/13/07, why? <ji*******@gmail.comwrote:
>
Evan Klitzke wrote:
On 6/12/07, why? <ji*******@gmail.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 <ev**@yelp.com>
Jun 13 '07 #4

P: n/a
On 13 Cze, 10:06, why? <jimbom...@gmail.comwrote:
Evan Klitzke wrote:
On 6/12/07, why? <jimbom...@gmail.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

Jun 13 '07 #5

P: n/a
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_input('Enter a number: '))
Enter a number: 35
>>y=int(raw_input('Enter a number: '))
Enter a number: 7
>>print 'sum is', sum(x,y)
sum is 42

Jun 14 '07 #6

P: n/a
Also, how can i save a file using text editor in linux?

Jun 14 '07 #7

P: n/a
En Thu, 14 Jun 2007 01:56:13 -0300, why? <ji*******@gmail.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_input('Enter a number: '))
Enter a number: 35
>>>y=int(raw_input('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

Jun 14 '07 #8

P: n/a
On Jun 13, 12:04 am, why? <jimbom...@gmail.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:

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

Jun 14 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.