13,420
edits
Changes
→PART 2 - Writing To Files
::#To demonstrate adding multiple lines, issue the following command:<source lang="python">
f.write('Line 1\nLine 2 is a little longer\nLine 3 is too\n')
</source><br>Once the '''write()''' operation has been run, the final step would be to '''close()''' the file. The file MUST be closed properly or else data will not consistently be written to the file. '''NOTE: Not closing a file can lead to corruption or not changes being made.''' <br><br>::#Issue the following command to close our file:<source lang="python">
f.close()
</source>
::#View Issue the following command to view the contents of the file to make sure the write data was saved. We can use the ipython bash alias %cat to do this.<source lang="python">
%cat file1.txt
Line 1