Module -3
Reading and Writing Files
Deepak B L
Assistant Professor
Dept. of Computer Science and Engineering
RLJIT, Bengaluru
Files and File Paths
A file has two key properties: a filename (usually written as one word) and a path.
The path specifies the location of a file on the computer. For example, there is a file on my
Windows 7 laptop with the filename [Link] in the path C:\Users\asweigart\
Documents.
The part of the filename after the last period is called the file’s extension and tells you a
file’s type. [Link] is a Word document, and Users, asweigart, and Documents all refer
to folders called directories. Folders can contain files and other folders. For example,
[Link] is in the Documents folder, which is inside the asweigart folder, which is inside
the Users folder. Figure shows this folder organization.
Cont..
The C:\ part of the path is the root folder, which contains all other folders.
On Windows, the root folder is named C:\ and is also called the C: drive.
On OS X and Linux, the root folder is /.
Backslash on Windows and Forward Slash on OS X and
Linux
• On Windows, paths are written using backslashes (\) as the separator between folder names.
OS X and Linux, however, use the forward slash (/) as their path separator. If you want your
programs to work on all operating systems, you will have to write your Python scripts to
handle both cases.
Fortunately, this is simple to do with the [Link]() function. If you pass it the string
values of individual file and folder names in your path, [Link]() will return a string with a
file path using the correct path separators. Enter the following into the interactive shell:
>>> import os
>>> [Link]('usr', 'bin', 'spam')
'usr\\bin\\spam'
Cont..
The [Link]() function is helpful if you need to create strings for filenames.
• For example, the following example joins names from a list of filenames to the end of a folder’s
name:
>>> myFiles = ['[Link]', '[Link]', '[Link]']
>>> for filename in myFiles:
print([Link]('C:\\Users\\asweigart', filename))
Output :
C:\Users\asweigart\[Link]
C:\Users\asweigart\[Link]
C:\Users\asweigart\[Link]
The Current Working Directory
• Every program that runs on your computer has a current working directory,
or cwd. Any filenames or paths that do not begin with the root folder are
assumed to be under the current working directory. You can get the current
working directory as a string value with the [Link]() function and change
it with [Link](). Enter the following into the interactive shell:
>>> import os
>>> [Link]()
'C:\\Python34'
>>> [Link]('C:\\Windows\\System32')
>>> [Link]()
'C:\\Windows\\System32'
Absolute vs. Relative Paths
There are two ways to specify a file path.
• An absolute path, which always begins with the root folder.
• A relative path, which is relative to the program’s current working directory.
There are also the dot (.) and dot-dot (..) folders. These are not real folders but
special names that can be used in a path.
A single period (“dot”) for a folder name is shorthand for “this directory.” Two
periods (“dot-dot”) means “the parent folder.”
The relative paths for folders and files in the
working directory C:\bacon
The .\ at the start of a relative path is optional.
For example, .\[Link] and [Link] refer to the same file.
Creating New Folders with [Link]()
• Your programs can create new folders (directories) with the [Link]() function.
Enter the following into the interactive shell:
>>> import os
>>> [Link]('C:\\delicious\\walnut\\waffles')
• This will create not just the C:\delicious folder but also a walnut folderinside C:\
delicious and a waffles folder inside C:\delicious\walnut. That is, [Link]() will
create any necessary intermediate folders in order to ensure that the full path exists.
The [Link] Module
• The [Link] module contains many helpful functions related to filenames
and file paths. For instance, you’ve already used [Link]() to build
paths in a way that will work on any operating system.
• The full documentation for the [Link] module is on the Python website at
[Link]
Handling Absolute and Relative Paths
• The [Link] module provides functions for returning the absolute path
of a relative path and for checking whether a given path is an absolute
path.
• Calling [Link](path) will return a string of the absolute path
of the argument. This is an easy way to convert a relative path into an
absolute one.
• Calling [Link](path) will return True if the argument is an
absolute path and False if it is a relative path.
Cont..
• Calling [Link](path, start) will return a string of a relative path from the start path
to path. If start is not provided, the current working directory is used as the start path.
Try these functions in the interactive shell:
>>> [Link]('.')
'C:\\Python34'
>>> [Link]('.\\Scripts')
'C:\\Python34\\Scripts'
>>> [Link]('.')
False
>>> [Link]([Link]('.'))
True
Cont..
• Enter the following calls to [Link]() into the interactive shell:
>>> [Link]('C:\\Windows', 'C:\\')
'Windows'
>>> [Link]('C:\\Windows', 'C:\\spam\\eggs')
'..\\..\\Windows'
>>> [Link]()
'C:\\Python34‘
• Calling [Link](path) will return a string of everything that comes
before the last slash in the path argument. Calling [Link](path) will
return a string of everything that comes after the last slash in the path argument.
• For example, enter the following into the interactive shell:
>>> [Link]([Link])
['C:', 'Windows', 'System32', '[Link]']
On OS X and Linux systems, there will be a blank string at the start of
the returned list:
>>> '/usr/bin'.split([Link])
['', 'usr', 'bin']
The split() string method will work to return a list of each part of the
path. It will work on any operating system if you pass it [Link].
Finding File Sizes and Folder Contents
Once you have ways of handling file paths, you can then start gathering
information about specific files and folders.
The [Link] module provides functions for finding the size of a file in bytes
and the files and folders inside a given folder.
Calling [Link](path) will return the size in bytes of the file in the
path argument.
Calling [Link](path) will return a list of filename strings for each file in
the path argument. (Note that this function is in the os module, not [Link].)
>>> [Link]('C:\\Windows\\System32\\[Link]')
776192
>>> [Link]('C:\\Windows\\System32')
['0409', '[Link]', '[Link]', '[Link]', '[Link]',
--snip--
'[Link]', '[Link]', 'zh-CN', 'zh-HK', 'zh-TW', '[Link]']
>>> totalSize = 0
>>> for filename in [Link]('C:\\Windows\\System32'):
totalSize = totalSize + [Link]([Link] ('C:\\Windows \\
System32', filename))
>>> print(totalSize)
1117846456
Checking Path Validity
Many Python functions will crash with an error if you supply them with a path that
does not exist. The [Link] module provides functions to check whether a given path
exists and whether it is a file or folder.
• Calling [Link](path) will return True if the file or folder referred to in the
argument exists and will return False if it does not exist.
• Calling [Link](path) will return True if the path argument exists and is a file
and will return False otherwise.
• Calling [Link](path) will return True if the path argument exists and is a folder
and will return False otherwise.
>>> [Link]('D:\\')
False
The File Reading/Writing Process
Once you are comfortable working with folders and relative paths, you’ll be
able to specify the location of files to read and write. The functions covered in
the next few sections will apply to plaintext files.
Plaintext files contain only basic text characters and do not include font, size,
or color information.
Text files with the .txt extension or Python script files with the .py extension
are examples of plaintext files.
These can be opened with Windows’s Notepad or OS X’s TextEdit application
• Binary files are all other file types, such as word processing documents,
PDFs, images, spreadsheets, and executable programs. If you open a binary
file in Notepad or TextEdit, it will look like scrambled nonsense, like in
Cont..
There are three steps to reading or writing files in Python.
1 Call the open() function to return a File object.
2. Call the read() or write() method on the File object.
3. Close the file by calling the close() method on the File object.
Opening Files with the open() Function
• To open a file with the open() function, you pass it a string path indicating the file
you want to open; it can be either an absolute or relative path. The open() function
returns a File object.
• Try it by creating a text file named [Link] using Notepad or TextEdit. Type Hello
world! as the content of this text file and save it in your user home folder. Then, if
you’re using Windows, enter the following into the interactive shell:
>>> helloFile = open('C:\\Users\\your_home_folder\\[Link]')
If you’re using OS X, enter the following into the interactive shell instead:
>>> helloFile = open('/Users/your_home_folder/[Link]')
Cont..
Both these commands will open the file in “reading plaintext” mode,
or read mode for short. When a file is opened in read mode, Python lets you
only read data from the file; you can’t write or modify it in any way.
Read mode is the default mode for files you open in Python. But if
you don’t want to rely on Python’s defaults, you can explicitly specify the
mode by passing the string value 'r' as a second argument to open().
So open('/Users/asweigart/[Link]', 'r') and open('/Users/asweigart/
[Link]') do the same thing.
Cont..
The call to open() returns a File object. A File object represents a file on your
computer; it is simply another type of value in Python, much like the lists and
dictionaries.
Reading the Contents of Files
Now that you have a File object, you can start reading from it. If you want to read the entire
contents of a file as a string value, use the File object’s read() method.
f = open("C:\\Users\\win8\\Desktop\\coding\[Link]", "r")
print([Link]())
Output:
When, in disgrace with fortune and men's eyes,
I all alone beweep my outcast state,
And trouble deaf heaven with my bootless cries,
And look upon myself and curse my fate,
Writing to Files
Python allows you to write content to a file in a way similar to how the print() function
“writes” strings to the screen.
You can’t write to a file you’ve opened in read mode, though. Instead, you need to open it in
“write plaintext” mode or “append plaintext” mode, or write mode and append mode for
short.
Pass 'w' as the second argument to open() to open the file in write mode. Append mode, on
the other hand, will append text to the end of the existing file.
Pass 'a' as the second argument to open() to open the file in append mode. If the filename
passed to open() does not exist, both write and append mode will create a new, blank file.
After reading or writing a file, call the close() method before opening the file again.
Write to an Existing File
• To write to an existing file, you must add a parameter to the open()
function:
• "a" - Append - will append to the end of the file
• "w" - Write - will overwrite any existing content
Create a New File
To create a new file in Python, use the open() method, with one of the
following parameters:
"x" - Create - will create a file, returns an error if the file exist
"a" - Append - will create a file if the specified file does not exist
"w" - Write - will create a file if the specified file does not exist
Delete a File
To delete a file, you must import the OS module, and run its [Link]()
function:
>>import os
>>[Link](“C:\\Users\\win8\\Desktop\\coding\[Link]")
Check if file exists, then delete it:
import os
if [Link]("[Link]"):
[Link]("[Link]")
else:
print("The file does not exist")
Delete Folder
• To delete an entire folder, use the [Link]() method:
>>import os
>>[Link]("myfolder")
Note: You can only remove empty folders.
Saving Variables with the shelve Module
You can save variables in your Python programs to binary shelf files using the
shelve module. This way, your program can restore data to variables from the
hard drive.
The shelve module will let you add Save and Open features to your
program. For example, if you ran a program and entered some configuration
settings, you could save those settings to a shelf file and then have the
program load them the next time it is run.
Cont..
>>> import shelve
>>> shelfFile = [Link]('mydata')
>>> cats = ['Zophie', 'Pooka', 'Simon']
>>> shelfFile['cats'] = cats
>>> [Link]()
>>> shelfFile = [Link]('mydata')
>>> list([Link]())
['cats']
>>> list([Link]())
[['Zophie', 'Pooka', 'Simon']]
>>> [Link]()
Saving Variables with the [Link]() Function
Recall from “Pretty Printing” that the [Link]() function will
“pretty print” the contents of a list or dictionary to the screen, while the
[Link]() function will return this same text as a string instead of
printing it.
Using [Link]() will give you a string that you can write to .py file.
This file will be your very own module that you can import whenever you
want to use the variable stored in it. For example, enter the following into
the interactive shell:
Cont..
>>> import pprint
>>> cats = [{'name': 'Zophie', 'desc': 'chubby'}, {'name': 'Pooka',
'desc': 'fluffy'}]
>>> [Link](cats)
"[{'desc': 'chubby', 'name': 'Zophie'}, {'desc': 'fluffy', 'name': 'Pooka'}]"
>>> fileObj = open('[Link]', 'w')
>>> [Link]('cats = ' + [Link](cats) + '\n')
83
>>> [Link]()