Dumb Q #1

Norm norm at norm.com
Wed Jan 29 05:50:43 CET 2003


These replies were great!  I've been reading python books every night, but
having live thoughts and explanations really help.

Could you / would you expound on what you mean in #1 of your answer?
iterate a slice of it, ie:   for record in records[:]:       I'm having
trouble visualizing it.

Thanks everyone!

Norm


"Andy Jewell" <andy at wild-flower.co.uk> wrote in message
news:mailman.1043758549.8656.python-list at python.org...
Norm,

Did you want to save the changes to the file?  If you did, you need to do
the
following:

1) Recombine fields into a colon-delimited string and put that back into the
relevant place in the records list.  You can't modify the list you'r
iterating safely, so iterate a slice of it, ie:

for record in records[:]:

2) Rewrite the records list back to the file.  Modifying the records /list/
merely changes the copy in memory.

** You probably want to back up the original before modifying it!

hope this helps,
-andyj

On Tuesday 28 Jan 2003 3:55 am, Norm wrote:
> I would like to open this unix passwd file and chang the GID of those who
> have a GID of 200 to 199
>
> This does everything except change fields[3] to 199
>
> Thoughts?
> Norm
>
> ##############################
> import sys
> try:
>     file = open ("passwd.txt" , "r+")
> except IOError, message:
>     print >> sys.stderr," Can't open the file:", message
>     sys.exit(1)
>
> records = file.readlines()
>
> for record in records:
>     fields = record.split(':')
>     if fields[3] == "200":
>         print fields[0], " has a GID of 200"
>         print "now changing it to 199"
>         fields[3] = 199
> file.close()
>
>
>
>
> -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
> http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
> -----==  Over 80,000 Newsgroups - 16 Different Servers! =-----






-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----==  Over 80,000 Newsgroups - 16 Different Servers! =-----




More information about the Python-list mailing list