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

Read .txt file like .py file

P: n/a
I have a text file and contents are:

Help="""
Code is written by xteam.
"""
value = 0.0
How do I read this file like python syntax. What I mean is first
readline operation should return complete declaration of 'Help'
variable. If I evaluate this string then it should create a 'Help'
variable with it's value.

May be something related to 'parsing' would help but I don't know much.
Jul 27 '08 #1
Share this Question
Share on Google+
2 Replies

P: n/a
King wrote:
I have a text file and contents are:

Help="""
Code is written by xteam.
"""
value = 0.0
How do I read this file like python syntax. What I mean is first
readline operation should return complete declaration of 'Help'
variable. If I evaluate this string then it should create a 'Help'
variable with it's value.
If you trust the author of the file and you're sure the code in the text file
isn't an Internet worm and won't delete all your files, you can get away with
the built-in "execfile" function.

If you want more security, look at the compiler module and the ast module.
They allow you to parse the file like a normal Python source file, and IIRC,
there are also ways to execute single statements from a syntax tree.

Stefan
Jul 27 '08 #2

P: n/a
Well, I have a look to into compiler module and gave it a try using
compiler.parseFile and compiler.walk but I haven't got what I need
here.

Jul 28 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.