[Tutor] deleting CR within files

Alan Gauld alan.gauld at blueyonder.co.uk
Sat Apr 17 13:09:40 EDT 2004


> Well, so now it is saying the the module object is not callable
referring to
> the for statement.
>    for i in dialog().readline:
>        in_file
>        out_file.write(I[:-1] + '\n')
> Is this because I am not able to call the dialog function from here?

Correct. The dialog function is inside the dialog module so
you have to specify it using module.function syntax:
ie. dialog.dialog()

> doesn't seem like it should be this difficult.  All I want to do is:
>
> 1. Read the file selected by the dialog() function.
> 2. Create a new file called filename_cleaned.txt
> 3. Write the file selected in step 1 to the file named in step 2
>             a.Take out the extra character returns from the file in
step 1
> 4. Close all the opened files.

Yes and thats all it takes. The problem seems to be in defining
exactly
what you want removed. Have you tried using the string strip() method
I suggested earlier? It is designed to remove crud from the end of a
line... It might be more reliable that the -1 slice technique.

> This is all I need to do and I have a strange suspicion that I am
doing WAY
> to much work for this.

All the Tkinter stuff is overkill. Indeed why botrher with the
dialog stuff, why not just ask thre user using raw_input()?

At least till you get it working. One of the fundamental principles
of good design is that you should separate the user interrace from
the logic. So if you write a function that cleans the file given
a filename, then capturing the filename becomes a separate
(and much easier) problem. Get the core of the program working
first then add the flashy GUI if you feel you need it.

The case study in my tutor gives an example of this, by building
a command line version 1st then adding a GUI later...

Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list