[Tutor] deleting CR within files

David Talaga dtalaga at novodynamics.com
Sat Apr 17 12:08:58 EDT 2004


>From the last post I sent I said that the problem is in the for statement. I
still think that.  But how would I take out the extra <cr>'s? Am I doing it
right?
  import os
  import Tkinter
  import sys
  import dialog

  root=Tkinter.Tk()
  result = 0
  def doDialog():
     global result
     result = dialog.dialog()

  f=Tkinter.Button(root, text="Find Files", command=doDialog)
  f.pack()
  x=Tkinter.Button(root, text="Close", command=sys.exit)
  x.pack()

  if f:
     fileName=dialog.dialog()
     in_file = open(fileName,'r').readlines()
     out_file = open(fileName + '_cleaned', 'w')
     for i in in_file: #Changed the for statement to say in_file (duh!)
        out_file.write(i[:-1] + '\n') #maybe the problem is here?
     print 'File cleaned'
     in_file.close()
     out_file.close()
     print filename



  root.mainloop()

  -----Original Message-----
  From: Alan Gauld [mailto:alan.gauld at blueyonder.co.uk]
  Sent: Friday, April 16, 2004 2:17 PM
  To: David Talaga; Roger Merchberger; tutor at python.org
  Subject: Re: [Tutor] deleting CR within files


  > root=Tkinter.Tk()
  > f=Tkinter.Button(root, text="Find Files",
  command=dialog.dialog).pack()
  > x=Tkinter.Button(root, text="Close", command=sys.exit).pack()

  Like another recent poster you are assigning the value of pack()
  to the variables. But pack returns None. In this case you don't
  use f or x so it doesn't matter but could lead to some strange
  things if you tried to use them later...

  > if f:

  Oops, sorry, you do use f, it will be false since pack() returns
  none wich is considered as false.

  As a result the block of code inside the if never gets executed.

  You need to define the widget then pack it:

  f=Tkinter.Button(root, text="Find Files", command=dialog.dialog)
  f.pack()

  The other problem is that oits not clear how you rtrieve the
  output from dialog.dialog. I suspect you need to wrap that in
  a local function that stores the value for you:

  result = 0
  def doDialog()
     global result
     result = dialog.dialog()

  Now pass doDialog into the command parameter and when the
  button is pressed the result will be stored in the global
  variable result. [ If you were using objects you would make
  the doDialog a method and store the result in an attribute
  of the class to avoid using a global variable...]

  HTH,

  Alan G
  Author of the Learn to Program web tutor
  http://www.freenetpages.co.uk/hp/alan.gauld
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040417/5235c63e/attachment.html


More information about the Tutor mailing list