Open main menu

CDOT Wiki β

Changes

OPS435 Python Lab 5

13 bytes added, 08:39, 5 September 2017
PART 2 - Writing To Files
== PART 2 - Writing To Files ==
:Up to this point, you have learned how to access data text from a file. In this section, you will learn how to write data text to a file. Writing data to a file is useful for creating new content in a file or updating (modifying) existing data contained within a file.
:'''Perform the Following Steps:'''
<blockquote style="margin-left:35px;">{{Admon/caution|style="padding-left:25px"|Risk of Losing File Contents|A common problem that new Python programmers may encounter is to accidentally erase existing contents of a file when writing new data to a file. When opening files for writing (using the ''''w'''' open function attributeoption), Python assumes existing content in the file is no longer wanted and it's immediately deleted; therefore, if you wish to write data to a file but keep existing content, it is recommended to use the open file attribute option ''''a'''' in order to append new data to a file.}}</blockquote>
::#To start, open the ipython3 shell:<source lang="python">
ipython3
f.close()
</source>
::#View Issue the following command to confirm that the contents of the were written to file2.txt to make sure the write data was saved.:<source lang="python">
cat file2.txt
</source>
13,420
edits