Dumb Q #1

Andy Jewell andy at wild-flower.co.uk
Tue Jan 28 07:51:46 EST 2003


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! =-----






More information about the Python-list mailing list