Import Issue

  • Thread starter praveenkumar.117
  • Start date
P

praveenkumar.117

Hi all,
I am facing a problem while importing a file in python
script.
After doing import file i am updating that file. Later i am
accessing a dictionary contained in that
file. Eventhough changes are reflected in the file... When i
access a dictionary those changes are
not there. I believe that it is accessing from the object
file that is created when i did import at
the start of the script. I will be kind enough if somebody
suggest solution to this problem.
Regards-
praveen
 
A

A.T.Hofkamp

Hi all,
After doing import file i am updating that file. Later i am
accessing a dictionary contained in that
file. Eventhough changes are reflected in the file... When i
access a dictionary those changes are
not there. I believe that it is accessing from the object
file that is created when i did import at
the start of the script. I will be kind enough if somebody
suggest solution to this problem.

The easiest suggestion I can make is "don't do that".
An import statement is intended for importing static Python code, not for
importing dynamic data such as dictionaries that you update while running.

Your application is much easier to understand if you keep Python code and data
in seperate files.
For this, there are several options:

A.
If you stick to Python syntax of your data, you can load such files with 'open'
and 'read', followed by 'eval'

fp = open('datafile','r')
datatext = fp.read()
fp.close()
print eval datatext

B.
If you don't care about accessing the data outside Python, you can use the
pickle module to load and save the data.
(please read http://docs.python.org/lib/module-pickle.html for details).

C.
Last but not least, you can define your own data format, and write custom load
and save routines for accessing the data file. This approach is highly
flexible, but it does cost effort to write the routines.


Hope this answer your question,
Albert
 
M

Maric Michaud

Le Vendredi 02 Juin 2006 08:42, (e-mail address removed) a écrit :
Hi all,
After doing import file i am updating that file. Later i am
accessing a dictionary contained in that
file. Eventhough changes are reflected in the file... When i
access a dictionary those changes are
not there. I believe that it is accessing from the object
file that is created when i did import at
the start of the script. I will be kind enough if somebody
suggest solution to this problem.

Read in the doc the reload builtin description to understand what happens.
But as Albert said before, don't do that !

--
_____________

Maric Michaud
_____________

Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
 

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,297
Messages
2,571,536
Members
48,282
Latest member
Xyprime

Latest Threads

Top