Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You are really confused on how to use a file.</p> <p>First of all, why are you doing <code>int(open(filename, "w"))</code>? To open a file for writing just use:</p> <pre><code>outfile = open(filename, "w") </code></pre> <p>Then files does not support item assignment, so doing <code>fileobject[key]</code> does not make sense. Also note that opening a file with <code>"w"</code> <em>deletes</em> the previous contents! So if you want to modify the contents of a file you should use <code>"r+"</code> instead of <code>"w"</code>. You then have to read the file and parse its contents. In your case is probably better to first read the contents and then create a new file to write the new contents.</p> <p>To write a list of numbers to a file do:</p> <pre><code>outfile.write(','.join(str(number) for number in list2)) </code></pre> <p><code>str(number)</code> "converts" an integer into its string representation. <code>','.join(iterable)</code> joins the elements in <em>iterable</em> using a comma as separator and <code>outfile.write(string)</code> writes <em>string</em> to the file.</p> <p>Also, put the import outside the function(at the beginning of the file possibly) and you do not need to repeat it everytime you use the module.</p> <p>A complete code could be:</p> <pre><code>import tkinter.filedialog def replace(): drawfilename = tkinter.filedialog.askopenfilename() # read the contents of the file with open(drawfilename, "r") as infile: numbers = [int(number) for number in infile.read().split(',')] del numbers[-3:] # with automatically closes the file after del numbers[-3:] input_list = input("Enter three numbers separated by commas: ") # you do not have to strip the spaces. int already ignores them new_numbers = [int(num) for num in input_list.split(',')] numbers = new_numbers + numbers #drawfilename = tkinter.filedialog.askopenfilename() if you want to reask the path # delete the old file and write the new content with open(drawfilename, "w") as outfile: outfile.write(','.join(str(number) for number in numbers)) </code></pre> <p>Update: If you want to deal with more than one sequence you can do this:</p> <pre><code>import tkinter.filedialog def replace(): drawfilename = tkinter.filedialog.askopenfilename() with open(drawfilename, "r") as infile: sequences = infile.read().split(None, 2)[:-1] # split(None, 2) splits on any whitespace and splits at most 2 times # which means that it returns a list of 3 elements: # the two sequences and the remaining line not splitted. # sequences = infile.read().split() if you want to "parse" all the line input_sequences = [] for sequence in sequences: numbers = [int(number) for number in sequence.split(',')] del numbers[-3:] input_list = input("Enter three numbers separated by commas: ") input_sequences.append([int(num) for num in input_list.split(',')]) #drawfilename = tkinter.filedialog.askopenfilename() if you want to reask the path with open(drawfilename, "w") as outfile: out_sequences = [] for sequence, in_sequence in zip(sequences, input_sequences): out_sequences.append(','.join(str(num) for num in (in_sequence + sequence))) outfile.write(' '.join(out_sequences)) </code></pre> <p>This should work with any number of sequences. Note that if you have an extra space somewhere you'll get wrong results. If possible I'd put these sequences on different lines.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload