[Tutor] deleting CR within files
David Talaga
dtalaga at novodynamics.com
Fri Apr 16 11:41:52 EDT 2004
Hello all, need some help again! Here is the code I have:
import os
import Tkinter
import dialog
import sys
#import re
root=Tkinter.Tk()
f=Tkinter.Button(root, text="Find Files", command=dialog.dialog).pack()
x=Tkinter.Button(root, text="Close", command=sys.exit).pack()
#sub = re.sub
if f:
fileName= dialog.dialog()
in_file = open(fileName,'r').readlines()
out_file = open(fileName + '_cleaned', 'w')
for i in dialog().readline:
out_file.write(i[:-1] + '\n')
print 'File cleaned'
#in_file.close()
out_file.close()
print filename
root.mainloop()
I get no error message when I run it but I also have a test document that I
tested it on and nothing happens. I did what you suggested below and I am
not getting anything. I figured I would put an if statement in there to
avoid the needing a string error for in_file =
open(fileName,'r').readlines(). So here is where I am at. Stuck. I can
not wait to be more proficient in this language!
-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org]On
Behalf Of Roger Merchberger
Sent: Thursday, April 15, 2004 1:18 PM
To: tutor at python.org
Subject: RE: [Tutor] deleting CR within files
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?????????????
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20040416/d85296db/attachment.html
More information about the Tutor
mailing list