Changes

Jump to: navigation, search

OPS435 Python Lab 5

58 bytes added, 07:25, 5 September 2017
PART 2 - Writing To Files
::#To confirm that the new file exists and is empty, issue the following command:<source lang="python">
ls -l file1.txt
</source><br>To add lines of text to the file, you would use the '''write()''' method for the '''file object'''. For safe file management, always end every line with the special character ''''\n'''' to represent a "new line". Multiple lines may also be placed inside a single write operation: simply put the special character ''''\n'''' wherever a line should end.<br><br>
::#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. ''' <source lang="python">
f.close()
</source>
13,420
edits

Navigation menu