Dumb Q #1
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.
"Andy Jewell" <andy at wild-flower.co.uk> wrote in message
news:mailman.1043758549.8656.python-list at python.org...
Did you want to save the changes to the file? If you did, you need to do
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,
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 to 199
> import sys
> file = open ("passwd.txt" , "r+")
> except IOError, message:
> print >> sys.stderr," Can't open the file:", message
> records = file.readlines()
> for record in records:
> fields = record.split(':')
> if fields == "200":
> print fields, " has a GID of 200"
> print "now changing it to 199"
> fields = 199
> -----= 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