Changes

Jump to: navigation, search

OPS435 Python3 Lab 5

238 bytes added, 23:59, 6 October 2019
PART 2 - Writing To Files
f = open('file1.txt', 'w')
</source>
:#To confirm that the new file now exists and is empty, start up another terminal window, 'cd' to the same directory ~/ops435/lab5, and issue the following shell command:<source lang="bash">
ls -l file1.txt
</source>To add lines of text to the file, you can use the '''write()''' method for of the '''file object'''. Typically you end every line in a text file 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>
:#Try adding multiple lines:<source lang="python">
f.write('Line 1\nLine 2 is a little longer\nLine 3 is too\n')
f.close()
</source>
:#View the contents of the file in the other terminal window and run the following shell command to make sure the data was written successfully:<source lang="bash">
cat file1.txt
</source>You will now create a new file called file2.txt, but this time run multiple call the write() methods method multiple times in sequence. You will often write to a file multiple times inside a loop:<br><source lang="python">
f = open('file2.txt', 'w')
f.write('Line 1\nLine 2 is a little longer\nLine 3 is as well\n')
f.close()
</source>
:#Issue the following shell command on the other terminal window to confirm that the contents were written to file2.txt:<source lang="bash">
cat file2.txt
</source>
<blockquote style="margin-left:35px;">{{Admon/important|style="padding-left:25px"|Make Backup Copies of Your Data Files|Since you might make a mistake, and accidentally destroy file contents when writing to your file, it is highly recommended to make backup copies of your files prior to running your Python scripts. This can be particularly useful when performing any future assignment involving writing to files.}}</blockquote>
<blockquote style="margin-left:35px;">{{Admon/caution|style="padding-left:25px"|Make Backup Copies of ALL your files|Since you are now writing code that opens files for writing you may accidentally truncate the wrong file (like your assignment file, for example). Make regular backups of all your work. Just copy it to a USB stick or if you're bored have time - learn version controluse git or github to backup.}}</blockquote><br>
<ol style="margin-left:80px;"><li value="7">Issue the following shell commands to backup both of your newly-created files and confirm backup:<source lang="bash">
cp file1.txt file1.txt.bk
f = open('file2.txt', 'w')
</source>
On the other terminal window:
<source lang="bash">
cat file2.txt
f.close()
</source>
On ther other terminal window:
<source lang="bash">
cat file1.txt
</source>The final thing to consider when writing to files is to make certain that the values object being written are '''strings'''. This means that before trying to place integers, floats, lists, or dictionaries into a file, first either convert the value using '''str()''' function or extract the specific strings from items in the list.<br><br></li>
<li>In this example we convert a single number and all the numbers in a list to strings before writing them to a file:<source lang="python">
my_number = 1000
# Takes two strings, reads data from first file, writes data to new file, adds line number to new file
</source>
:# '''Replace''' the main section coding block of your Python script near the bottom with the following:<source lang="python">
if __name__ == '__main__':
file1 = 'seneca1.txt'
cd ~/ops435/lab5/
pwd #confirm that you are in the right directory
ls CheckLab5.py || wget https://rawict.githubusercontentsenecacollege.comca/Seneca-CDOT~raymond.chan/ops435/masterlabs/LabCheckScripts/CheckLab5.py
python3 ./CheckLab5.py -f -v lab5b
</source>
1,760
edits

Navigation menu