<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=File_Operations</id>
	<title>File Operations - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://training-course-material.com/index.php?action=history&amp;feed=atom&amp;title=File_Operations"/>
	<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=File_Operations&amp;action=history"/>
	<updated>2026-05-14T01:01:27Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://training-course-material.com/index.php?title=File_Operations&amp;diff=9211&amp;oldid=prev</id>
		<title>Kristian Rother at 20:32, 5 February 2013</title>
		<link rel="alternate" type="text/html" href="https://training-course-material.com/index.php?title=File_Operations&amp;diff=9211&amp;oldid=prev"/>
		<updated>2013-02-05T20:32:14Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Cat|Python Commands}}&lt;br /&gt;
{{Python Links}}&lt;br /&gt;
&lt;br /&gt;
== Opening files for reading ==&lt;br /&gt;
Text files can be accessed using the open() function. It returns an open file from which its contents can be extracted as a string.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
f = open(&amp;#039;my_file.txt&amp;#039;)&lt;br /&gt;
text = f.read()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Opening files for writing ==&lt;br /&gt;
Writing text to files is very similar. The only difference is the &amp;#039;w&amp;#039; parameter.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
f = open(&amp;#039;my_file.txt&amp;#039;,&amp;#039;w&amp;#039;)&lt;br /&gt;
f.write(text)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Appending to files ==&lt;br /&gt;
It is possible to add text to an existing file, too.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
f = open(&amp;#039;my_file.txt&amp;#039;,&amp;#039;a&amp;#039;)&lt;br /&gt;
f.write(text)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Closing files ==&lt;br /&gt;
Closing files in Python is not mandatory.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
f.close()&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Tips for reading files: ==&lt;br /&gt;
An open file behaves like a list of strings. Thus, it is possible to read it line by line without using s.split(&amp;#039;\n&amp;#039;):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
for line in open(&amp;#039;my_file.txt&amp;#039;):&lt;br /&gt;
name = line[:10].strip()&lt;br /&gt;
print name&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Tips for writing files: ==&lt;br /&gt;
Analogously, if you have data as a list of strings, it can be written to a file as a one-liner. You should remember to have linebreaks:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
lines = [&amp;#039;first line\n&amp;#039;,&lt;br /&gt;
&amp;#039;second line\n&amp;#039;]&lt;br /&gt;
open(&amp;#039;my_file.txt&amp;#039;,&amp;#039;w&amp;#039;).writelines(lines)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Take care of IOErrors ==&lt;br /&gt;
You should keep in the back of your head that opening a non-existing file for reading causes an IOError. Eventually, a try.. except clause is useful, then (but not always).&lt;br /&gt;
&lt;br /&gt;
== Writing directory names in Python ==&lt;br /&gt;
When opening files, you can also use full or relative directory names. However, you must replace the backslash &amp;#039;\&amp;#039; by a double backslash &amp;#039;\\&amp;#039; (because &amp;#039;\&amp;#039; is also used for &amp;#039;\n&amp;#039; and &amp;#039;\t&amp;#039;).&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
f = open(&amp;#039;..\\my_file.txt&amp;#039;)&lt;br /&gt;
f = open(&amp;#039;C:\\python\\my_file.txt&amp;#039;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Managing directories ==&lt;br /&gt;
With the os module, you can change to a different directory:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import os&lt;br /&gt;
os.chdir(&amp;#039;&amp;#039;..\\python&amp;#039;&amp;#039;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
You can also get a list with all files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
os.listdir(&amp;#039;&amp;#039;..\\python&amp;#039;&amp;#039;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The third most important function is for checking whether a file exists:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
print os.access(&amp;#039;my_file.txt&amp;#039;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== The CSV module ==&lt;br /&gt;
An open file behaves like a list of strings. Thus, it is possible to read it line by line without using&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import csv&lt;br /&gt;
reader = csv.reader(open(&amp;#039;my_file.csv&amp;#039;))&lt;br /&gt;
for row in reader:&lt;br /&gt;
print row&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Similarly, tables can be written to files:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
writer = csv.writer(open(&amp;#039;my_file.csv&amp;#039;,&amp;#039;w&amp;#039;))&lt;br /&gt;
writer.writerow(table)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Options of CSV file readers/writers ==&lt;br /&gt;
Both CSV readers and writers can handle a lot of different formats. Options you can change include:&lt;br /&gt;
* delimiter: the symbol separating columns.&lt;br /&gt;
* quotechar: the symbol used to quote strings.&lt;br /&gt;
* lineterminator: the symbol at the end of lines.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;source lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
reader = csv.reader(open(&amp;#039;my_file.csv&amp;#039;),&lt;br /&gt;
delimiter=&amp;#039;\t&amp;#039;, quotechar=&amp;#039;”&amp;#039;)&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Kristian Rother</name></author>
	</entry>
</feed>