{article Dive into Python}{title} {text} {/article}

Writing to Files

Python 2.7.6 Writing to Files

Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> logfile = open('test.log', 'w')
>>> logfile.write('test succeeded')
>>> logfile.close()
>>> print file('test.log').read()
test succeeded
>>> logfile = open('test.log', 'a')
>>> logfile.write('line 2')
>>> logfile.close()
>>> print file('test.log').read()
test succeededline 2
>>>

You start boldly by creating either the new file test.log or overwrites the existing file, and opening the file for writing. (The second parameter "w" means open the file for writing.) Yes, that's all as dangerous as it sounds. I hope you didn't care about the previous contents of that file, because it's gone now.

You can add data to the newly opened file with the write method of the file object returned by open.

file is a synonym for open. This one−liner opens the file, reads its contents, and prints them.

You happen to know that test.log exists (since you just finished writing to it), so you can open it and append to it. (The "a" parameter means open the file for appending.) Actually you could do this even if the file didn't exist, because opening the file for appending will create the file if necessary. But appending will never harm the existing contents of the file.

As you can see, both the original line you wrote and the second line you appended are now in test.log. Also note that carriage returns are not included. Since you didn't write them explicitly to the file either time, the file doesn't include them. You can write a carriage return with the "\n" character.
Since you didn't do this, everything you wrote to the file ended up smooshed together on the same line.

{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> logfile = open('test.log', 'w')
>>> logfile.write('test succeeded')
>>> logfile.close()
>>> print file('test.log').read()
test succeeded
>>> logfile = open('test.log', 'a')
>>> logfile.write('line 2')
>>> logfile.close()
>>> print file('test.log').read()
test succeededline 2
>>>

</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}

Python 3.4.1 Writing to Files

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> logfile = open('test34.log', 'w', encoding='utf-8')
>>> logfile.name
'test34.log'
>>> logfile.encoding
'utf-8'
>>> logfile.mode
'w'
>>> logfile.write('test succeeded')
14
>>> logfile = open('test34.log', 'r', encoding='utf-8')
>>> logfile.read()
'test succeeded'
>>> logfile = open('test34.log', 'a', encoding='utf-8')
>>> logfile.write(' Python 3.4.1')
13
>>> logfile = open('test34.log', 'r', encoding='utf-8')
>>> logfile.read()
'test succeeded Python 3.4.1'
>>> logfile.close()
>>>

{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> logfile = open('test34.log', 'w', encoding='utf-8')
>>> logfile.name
'test34.log'
>>> logfile.encoding
'utf-8'
>>> logfile.mode
'w'
>>> logfile.write('test succeeded')
14
>>> logfile = open('test34.log', 'r', encoding='utf-8')
>>> logfile.read()
'test succeeded'
>>> logfile = open('test34.log', 'a', encoding='utf-8')
>>> logfile.write(' Python 3.4.1')
13
>>> logfile = open('test34.log', 'r', encoding='utf-8')
>>> logfile.read()
'test succeeded Python 3.4.1'
>>> logfile.close()
>>>

</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}

You can use the with statement and let Python close the file for you.

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> with open('test.log', mode='w', encoding='utf-8') as logfile:
logfile.write('with test succeeded')


19
>>> with open('test.log', encoding='utf-8') as logfile:
print(logfile.read())


with test succeeded
>>> with open('test.log', mode='a', encoding='utf-8') as logfile:
logfile.write(' Python 3.4.1')


13
>>> with open('test.log', encoding='utf-8') as logfile:
print(logfile.read())


with test succeeded Python 3.4.1
>>>

{source}
<!-- You can place html anywhere within the source tags -->
<pre class="brush:py;">
Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> with open('test.log', mode='w', encoding='utf-8') as logfile:
        logfile.write('with test succeeded')


19
>>> with open('test.log', encoding='utf-8') as logfile:
        print(logfile.read())


with test succeeded
>>> with open('test.log', mode='a', encoding='utf-8') as logfile:
        logfile.write(' Python 3.4.1')


13
>>> with open('test.log', encoding='utf-8') as logfile:
        print(logfile.read())


with test succeeded Python 3.4.1
>>>

</pre>

<script language="javascript" type="text/javascript">
    // You can place JavaScript like this

</script>
<?php
    // You can place PHP like this

?>
{/source}