[Tutor] newbie: Reading text file
Alan Gauld
alan.gauld at btinternet.com
Fri Jun 1 09:31:59 CEST 2007
"Preecha Bundrikwong" <preecha88 at gmail.com> wrote
> I have a text file (mylist.py actually), it contains exactly below:
> ---------------
> # file mylist.py
> jobs = [
> 'Lions',
> 'SysTest',
> ]
> ----------------
>
> I want to write another script and get the list "jobs" from the
> above
> script.
Because this is a python file and python files are effectively
modules you can use import:
import mylist
print mylist.jobs
> Is it possible for me to get the variable 'jobs' and its value from
> the file
> (as a list)? What I'm trying to do now is open the file, readlines
> in a
> loop, once it finds the line with "jobs = [", then start accumulate
> (append)
> a varible until it hits the line with "]" then stop.
If it wasn't a python file (some other language say) then that is
exactly what you would need to do. Programming often makes
apparently simple things seem complicated! :-)
Alan G.
More information about the Tutor
mailing list