[Tutor] deleting CR within files
Roger Merchberger
zmerch at 30below.com
Thu Apr 15 13:18:20 EDT 2004
Rumor has it that David Talaga may have mentioned these words:
import os, Tkinter, dialog, sys
# You weren't using re anymore, no need to import it
root=Tkinter.Tk()
f=Tkinter.Button(root, text="Find Files", command=dialog.dialog).pack()
x=Tkinter.Button(root, text="Close", command=sys.exit(0)).pack()
# you didn't use 'sub' anymore, so...
# sub = re.sub
fileName= dialog.dialog
# if you call readlines() on the open, it opens, reads, and
# closes all in one step, it makes readability easier...
in_file = open(fileName,'r').readlines()
# See my previous post about how you can put the _cleaned *before*
# the extension so you can still open the files by extension...
out_file = open(fileName + '_cleaned', 'w')
for i in in_file:
# There's a non-syntax bug in the next line: if you need to remove
# the last character in the line, use -1. A negative index means:
# "Go from the end of the array (in this case an array of chars)
# and work backwards."
# what you coded was "Take the first character of every string,
# lop off the rest, and add a \n." Prolly not what you wanted. ;-)
out_file.write(i[:-1] + '\n')
# Remember, this is not actually searching for '\r' chars - it's only
# lopping off the last character, and adding a \n. If this is in
# winders, this won't work, as the standard line ending is
# '\r\n' - and the code above is lopping off a \n to add a \n.
# out_file.write(i[:-2] + '\n') # is what you'd want for winders.
print 'File cleaned'
# Python is "indent-loop" based - you'll be printing 'File cleaned'
# for *every line* in the file, not only at the end of the file.
# You'd wanna move that out of the loop by unindenting it.
# Next line not necessary if you readlines() with the open...
# in_file.close()
out_file.close()
# I haven't done a lot of Tk programming, but shouldn't the next
# line be:
root.mainloop()
# instead of Tkinter.mainloop?
=-=-=-=
My (so far one and only) main Tkinter script I wrote is here:
http://tivo.30below.com/zmerch/AviSynth_2.pyw
It's been tested on Python 2.2.2 and 2.3.x - and it's for a winders platform...
[snip]
>Now when I run it is says this:
>Traceback (most recent call last):
> File "<pyshell#0>", line 1, in ?
> execfile('crgui.py')
> File "crgui.py", line 13, in ?
> in_file = open(fileName,'r')
>TypeError: coercing to Unicode: need string or buffer, function found
For this error, what I'd do is right before this line, put in this:
=-=-=-=-=
print fileName
sys.exit(0)
=-=-=-=-=
If what prints looks like: u'ThisIsUnicode.txt'
^^
starting with a U, then the dialog.dialog script
is returning a unicode string instead of standard
ASCII string, which is what's necessary to open files.
Try this line instead:
fileName= str(dialog.dialog)
That may convert the unicode to standard ASCII.
HTH,
Roger "Merch" Merchberger
--
Roger "Merch" Merchberger -- sysadmin, Iceberg Computers
zmerch at 30below.com
What do you do when Life gives you lemons,
and you don't *like* lemonade?????????????
More information about the Tutor
mailing list