1

I wanted to make a text file concatentation/duplicate line removal program for some practice, and I'm just about ready to turn my computer off and wait for the singularity because I have no worldly idea as to why this code is behaving the way it is.

The kicker is I had pretty much made it. I had the script done, it was working fine. It merged files, the duplicate detection worked... Then I added comments, and it all went crazy. It used to go through a list of text files, open each of them, and read their lines into the merged file. For testing I have two files, wl1.txt and wl2.txt. These are their contents:

wl1.txt:

test
test1
word
word1
wordword
entry
python
random

wl2.txt:

yellow
red
bluw
test
test1
random
black
Black

This is the snippet of code that I am running to isolate this incident:

fileList = ['C:\lists\wl\wl1.txt','C:\lists\wl\wl2.txt']
outfile = open("C:\lists\wl\wlmerge.txt",'w+')

for fname in fileList:
    infile = open(fname,"r")                                                    
    for line in infile:
        print line                                                         
        outfile.write(line)                                                
    print outfile.read()

Before running the code wlmerge.txt did not exist and there are no other lines in the input text files than the ones I've listed. And this, is the output I get:

test

test1

word

word1

wordword

entry

python

random
mat__': 1, 'runit': 1, 'remove_selection': 1, '__str__': 1, '_file_line_helper': 1, '_asktabwidth': 1, '_filename_to_unicode': 1, 'open_stack_viewer': 1, 'get_region': 1, 'cut': 1, 'open_module': 1, 'showerror': 1, '__class__': 1, 'smart_indent_event': 1, 'set_status_bar': 1, 'about_dialog': 1, 'indent_region_event': 1, 'load_extension': 1, 'set_region': 1, '_close': 1, 'cancel_callback': 1, 'postwindowsmenu': 1, '__subclasshook__': 1, 'newline_and_indent_event': 1, 'toggle_debugger': 1, 'saved_change_hook': 1, 'eof_callback': 1, 'get_warning_stream': 1, 'get_standard_extension_names': 1, 'stop_readline': 1, 'guess_indent': 1, 'ResetFont': 1, 'rmenu_check_paste': 1, 'replace_event': 1, 'unload_extensions': 1, 'del_word_right': 1, 'close_debugger': 1, '_EditorWindow__extra_help_callback': 1, 'python_docs': 1, 'fill_menus': 1, 'flush': 1, 'close': 1, 'center_insert_event': 1, '__setattr__': 1, 'set_notabs_indentwidth': 1, 'help_dialog': 1, 'set_saved': 1, 'get_selection_indices': 1, 'open_debugger': 1, 'tabify_region_event': 1, 'comment_region_event': 1, 'get_var_obj': 1, 'find_selection_event': 1, '_rmcolorizer': 1, 'goto_line_event': 1, 'load_standard_extensions': 1, 'reset_undo': 1, 'long_title': 1, 'paste': 1, 'close2': 1, 'reset_help_menu_entries': 1, 'set_indentation_params': 1, 'open_class_browser': 1, 'endexecuting': 1, 'rmenu_check_cut': 1, '__delattr__': 1, '_addcolorizer': 1, '__repr__': 1, 'close_hook': 1, 'home_callback': 1, 'right_menu_event': 1, 'getlineno': 1, 'apply_bindings': 1, 'restart_shell': 1, '_make_blanks': 1, 'get_geometry': 1, 'ApplyKeybindings': 1, 'get_tabwidth': 1, 'ResetColorizer': 1, 'open_path_browser': 1, 'filename_change_hook': 1, '_build_char_in_string_func': 1, 'isatty': 1, 'find_event': 1, 'set_close_hook': 1, '__reduce__': 1, 'find_in_files_event': 1, 'untabify_region_event': 1, 'new_callback': 1, 'getvar': 1, 'copy': 1, 'rmenu_check_copy': 1, 'center': 1, 'writelines': 1, 'recall': 1, 'load_extensions': 1, 'showprompt': 1, 'close_event': 1, 'reindent_to': 1, 'askinteger': 1, '__hash__': 1, 'RemoveKeybindings': 1, 'dedent_region_event': 1, 'linefeed_callback': 1, 'is_char_in_string': 1, '__getattribute__': 1, 'move_at_edge_if_selection': 1, 'beginexecuting': 1, 'enter_callback': 1, 'short_title': 1, 'getwindowlines': 1, 'smart_backspace_event': 1, '__sizeof__': 1, 'set_tabwidth': 1, 'find_again_event': 1, '__init__': 1, 'del_word_left': 1, 'get_saved': 1, '__reduce_ex__': 1, '__new__': 1, 'select_all': 1, 'gotoline': 1, 'view_restart_mark': 1, 'change_indentwidth_event': 1, 'write': 1, 'set_debugger_indicator': 1, 'config_dialog': 1, 'set_warning_stream': 1, 'setvar': 1, 'createmenubar': 1, 'begin': 1, 'toggle_tabs_event': 1, 'askyesno': 1, 'ispythonsource': 1, 'resetoutput': 1, 'goto_file_line': 1, 'readline': 1, 'toggle_jit_stack_viewer': 1, 'make_rmenu': 1, '_EditorWindow__recent_file_callback': 1, 'uncomment_region_event': 1, 'update_recent_files_list': 1, 'set_line_and_column': 1})
find_again_event'
p118
I1
sS'__init__'
p119
I1
sS'del_word_left'
p120
I1
sS'askyesno'
p121
I1
sS'__reduce_ex__'
p122
I1
sS'__new__'
p123
I1
sS'getlineno'
p124
I1
sS'__format__'
p125
I1
sS'view_restart_mark'
p126
I1
sS'change_indentwidth_event'
p127
I1
sS'write'
p128
I1
sS'set_debugger_indicator'
p129
I1
sS'config_dialog'
p130
I1
sS'set_warning_stream'
p131
I1
sS'setvar'
p132
I1
sS'createmenubar'
p133
I1
sS'begin'
p134
I1
sS'toggle_tabs_event'
p135
I1
sS'help_dialog'
p136
I1
sS'ispythonsource'
p137
I1
sS'resetoutput'
p138
I1
sS'goto_file_line'
p139
I1
sS'readline'
p140
I1
sS'toggle_jit_stack_viewer'
p141
I1
sS'make_rmenu'
p142
I1
sS'center'
p143
I1
sS'uncomment_region_event'
p144
I1
sS'short_title'
p145
I1
sS'set_line_and_column'
p146
I1
stp147
tp148
.

Any help would be greatly appreciated.

2 Answers 2

1

You're opening the outfile with a mode of w+, which will truncate the output. You should get rid of the print outfile.read() line as the outfile is already at the end of the buffer, so read() is not doing what you think it should do.

If you really want to read the outfile after you have written to it (but still in your loop over files), you'll need to rewind to the start of the file. You can do this with outfile.seek(0).

If you want to open outfile as both read and write without truncation, you should use r+ as the mode.

Sign up to request clarification or add additional context in comments.

4 Comments

Well, yes, it is valid according to the documentation While r+ does indeed open the file for reading and writing that only works if the file already exists, which in this case it does not. w+ will open/create the file for read and write, as well as truncating it, which is ideal for this situation. I gave it a shot with r+ and an empty file though, same results.
Ah I see, I didn't notice it on this tutorial page of the modes. In any case, print outfile.read() probably doesn't do what you want because you are already at the end of the buffer.
Okay, that could very well be the reason this is happening then. I'm looking through the documentation now, but do you know of the best way to go about returning the buffer back to the start?
outfile.seek(0) will rewind to the start of the file. I just updated the answer to reflect our further discussion.
1

Just delete the print outfile.read() line:

fileList = ['wl1.txt','wl2.txt']
outfile = open("wlmerge.txt",'w+')

for fname in fileList:
    infile = open(fname,"r")                                                    
    for line in infile:
        print line                                                         
        outfile.write(line)                 

This works fine for me.

Edit

To read it after writing:

outfile.close()
with open('wlmerge.txt', 'r') as f:
    read_data = f.read()

print read_data

1 Comment

But reading the file after adding those lines is essential to my program. How should I go about reading the wlmerge file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.