Changes

Jump to: navigation, search

OPS435 Python Lab 5

15 bytes added, 08:52, 5 September 2017
PART 2 - Writing To Files
f.write('Last line in file\n')
f.close()
cat file1.txt</source><br>The final point thing to make consider when writing to files is to make certain that the values 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>To see how to convert numbers into strings to be stored into a file, issue the following commands:<source lang="python">
my_number = 1000
f = open('file3.txt', 'w')
f.write(str(my_number) + '\n')
for num in my_lismy_list
f.write(str(num) + '\n')
f.close()
13,420
edits

Navigation menu