Remembering

M

MComp

Hey guys, I'm another new to programming guy. I have one quick
question. How do you make a program not forget everything you input,
once it shuts down? Thanks in advance.
 
A

Ahmed MOHAMED ALI

Hi,
If you talk about your python code ,type your instructions
in a file.If you talk about your data ,you must use a persistence system
(file,database ...)

Ahmed
 
M

Marcos Eimil Pardo

MComp escribió:
Hey guys, I'm another new to programming guy. I have one quick
question. How do you make a program not forget everything you input,
once it shuts down? Thanks in advance.

you can save read data to a file:

data=raw_input("type some data> ")
# open a file to write text
f=file("savedata","wt")
# save data to file
f.write(data)
# close file
f.close()

and when you reload your program you can read data from file:

# open a file to read text
f=file("savedata","rt")
# save data to file
data=f.read()
# close file
f.close()


well this is a very simplistic solution, if you want to save multiple data, you
must save it following some convention, for example if you have read name and
age, you can save it in a text file, with two lines, the first with name and the
second with age, and when you restart your program you should read from file in
the same order:

#to write multiple data item on multiple lines
name=raw_input("name> ")
age=raw_input("age> ")
# open a file to write text
f=file("savedata","wt")
# save name to file
f.write(name)
# write newline character
f.write("\n")
# save age to file
f.write(age)
# close file
f.close()


#to read multiple data item on multiple lines
# open a file to read text
f=file("savedata","rt")
# read name from file (name is in first line)
# attention: we use readline to read one line, read() gets entire
# file (read manual for more information)
name = f.readline()
# read age from file (age is in second line)
age = f.readline()
# age and name are strings terminated with a new-line character,
# we may want to strip this last char, we can do it this way:
age = age[:-1]
name = name[:-1]
# close file
f.close()

hope, gives some help.
greets, marcos.
 
M

Marcos Eimil Pardo

Marcos Eimil Pardo escribió:
MComp escribió:
Hey guys, I'm another new to programming guy. I have one quick
question. How do you make a program not forget everything you input,
once it shuts down? Thanks in advance.


you can save read data to a file:

data=raw_input("type some data> ")
# open a file to write text
f=file("savedata","wt")
# save data to file
f.write(data)
# close file
f.close()

and when you reload your program you can read data from file:

# open a file to read text
f=file("savedata","rt")
# save data to file
data=f.read()
# close file
f.close()


well this is a very simplistic solution, if you want to save multiple
data, you must save it following some convention, for example if you
have read name and age, you can save it in a text file, with two lines,
the first with name and the second with age, and when you restart your
program you should read from file in the same order:

#to write multiple data item on multiple lines
name=raw_input("name> ")
age=raw_input("age> ")
# open a file to write text
f=file("savedata","wt")
# save name to file
f.write(name)
# write newline character
f.write("\n")
# save age to file
f.write(age)
# close file
f.close()


#to read multiple data item on multiple lines
# open a file to read text
f=file("savedata","rt")
# read name from file (name is in first line)
# attention: we use readline to read one line, read() gets entire
# file (read manual for more information)
name = f.readline()
# read age from file (age is in second line)
age = f.readline()
# age and name are strings terminated with a new-line character,
# we may want to strip this last char, we can do it this way:
age = age[:-1]
#oops too fast answer you can do better this way: (eliminating
#trailing whitespace characters, if present)
age=age.rstrip()
name = name[:-1] age=age.rstrip()

# close file
f.close()

hope, gives some help.
greets, marcos.
 
J

Jarek Zgoda

MComp said:
Hey guys, I'm another new to programming guy. I have one quick
question. How do you make a program not forget everything you input,
once it shuts down? Thanks in advance.

We usually save The Things in some safe place. Like user's HDD, for
example.
 
L

Lonnie Princehouse

I wrote a simple persistent dictionary class that may be of use
to you. I find it quite handy.

http://magicpeacefarm.com/lonnie/py/diskt.py

Basically, it uses pickle to store the contents of the dictionary
in a directory on the filesystem instead of in memory.
Consider this example:

Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from diskt import diskt
stuff = diskt()
stuff['foo'] = 'bar'

.... exit Python and restart ...

Python 2.2.2 (#1, Feb 24 2003, 19:13:11)
[GCC 3.2.2 20030222 (Red Hat Linux 3.2.2-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
from diskt import diskt
stuff = diskt()
print stuff['foo']
bar


(much lighter-weight than things like the ZODB :p)
 
J

John J. Lee

Hey guys, I'm another new to programming guy. I have one quick
question. How do you make a program not forget everything you input,
once it shuts down? Thanks in advance.

Once you've tried simple file manipulation (opening, reading, writing,
closing, and some of the stuff in os.path), look at the pickle module.

Don't get seduced by it too early, though, or you'll skip the learning
experience of struggling with parsing text files, which is good for
your soul ;-)


John
 
M

MComp

Thank you guys for all of your help. It really does help me out a lot. Thx again.
Monroe
 

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,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top