473,372 Members | 1,197 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

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!

Jun 13 '07 #1
8 19464
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

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
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
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
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
Also, how can i save a file using text editor in linux?

Jun 14 '07 #7
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Andrew Dalke | last post by:
Is there an author index for the new version of the Python cookbook? As a contributor I got my comp version delivered today and my ego wanted some gratification. I couldn't find my entries. ...
53
by: john67 | last post by:
The company I work for is about to embark on developing a commercial application that will cost us tens-of-millions to develop. When all is said and done it will have thousands of business...
1
by: brenny | last post by:
Hello I want to save a image to specified path as a jpg format. But I'm getting some error messages. Codes are shown below ; path ="C:\\image.jpg"; FileStream fs = new...
5
by: peter | last post by:
Hello all, I'm looking for an advice. Example (one block in ascii file): $------------------------ NAME='ALFA' CODE='x' $------------------------
2
by: koolest1 | last post by:
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...
1
by: openuser | last post by:
Hello, I've been researching how to embed python into C/C++. And, I learned how to write c/c++ code that, in threory, should do its job in embedding Python module into itself. Here is what I have.....
18
by: Jens | last post by:
I'm starting a project in data mining, and I'm considering Python and Java as possible platforms. I'm conserned by performance. Most benchmarks report that Java is about 10-15 times faster than...
0
by: Edwin.Madari | last post by:
since choice of dbm used by shelve http://docs.python.org/lib/node327.html depends on os, and whats available on it, shevle files saved on one os, most likely do not work on another os, sometimes on...
3
by: evenlater | last post by:
I have an Access application on a terminal server. Sometimes my users need to export reports to pdf, rtf or xls files and save them to their own client device hard drives. They can do that right...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.