Module - 3
Module - 3
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
Documents.
The part of the filename after the last period is called the file’s extension and tells you a
file’s type. project.docx is a Word document, and Users, asweigart, and Documents all refer
to folders called directories. Folders can contain files and other folders. For example,
project.docx 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 os.path.join() function. If you pass it the string
values of individual file and folder names in your path, os.path.join() will return a string with a
file path using the correct path separators. Enter the following into the interactive shell:
>>> import os
>>> os.path.join('usr', 'bin', 'spam')
'usr\\bin\\spam'
Cont..
The os.path.join() 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:
print(os.path.join('C:\\Users\\asweigart', filename))
Output :
C:\Users\asweigart\accounts.txt
C:\Users\asweigart\details.csv
C:\Users\asweigart\invite.docx
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 os.getcwd() function and change
it with os.chdir(). Enter the following into the interactive shell:
>>> import os
>>> os.getcwd()
'C:\\Python34'
>>> os.chdir('C:\\Windows\\System32')
>>> os.getcwd()
'C:\\Windows\\System32'
Absolute vs. Relative Paths
There are two ways to specify a file path.
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
• Your programs can create new folders (directories) with the os.makedirs() function.
Enter the following into the interactive shell:
>>> import os
>>> os.makedirs('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, os.makedirs() will
create any necessary intermediate folders in order to ensure that the full path exists.
The os.path Module
• The os.path module contains many helpful functions related to filenames
and file paths. For instance, you’ve already used os.path.join() to build
paths in a way that will work on any operating system.
• The full documentation for the os.path module is on the Python website at
http://docs.python.org/3/library/os.path.html.
Handling Absolute and Relative Paths
• The os.path module provides functions for returning the absolute path
of a relative path and for checking whether a given path is an absolute
path.
• Calling os.path.relpath(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.
The os.path module provides functions for finding the size of a file in bytes
and the files and folders inside a given folder.
Calling os.path.getsize(path) will return the size in bytes of the file in the
path argument.
Calling os.listdir(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 os.path.)
>>> os.path.getsize('C:\\Windows\\System32\\calc.exe')
776192
>>> os.listdir('C:\\Windows\\System32')
['0409', '12520437.cpx', '12520850.cpx', '5U877.ax', 'aaclient.dll',
--snip--
'xwtpdui.dll', 'xwtpw32.dll', 'zh-CN', 'zh-HK', 'zh-TW', 'zipfldr.dll']
>>> totalSize = 0
>>> for filename in os.listdir('C:\\Windows\\System32'):
totalSize = totalSize + os.path.getsize(os.path.join ('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 os.path module provides functions to check whether a given path
exists and whether it is a file or folder.
• Calling os.path.exists(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 os.path.isfile(path) will return True if the path argument exists and is a file
and will return False otherwise.
• Calling os.path.isdir(path) will return True if the path argument exists and is a folder
and will return False otherwise.
>>> os.path.exists('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.
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 hello.txt 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:
If you’re using OS X, enter the following into the interactive shell instead:
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().
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\hello.txt", "r")
print(f.read())
Output:
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
"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 os.remove()
function:
>>import os
>>os.remove(“C:\\Users\\win8\\Desktop\\coding\hello2.txt")
import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
Delete Folder
>>import os
>>os.rmdir("myfolder")
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 = shelve.open('mydata')
>>> cats = ['Zophie', 'Pooka', 'Simon']
>>> shelfFile['cats'] = cats
>>> shelfFile.close()
Using pprint.pformat() 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'}]
>>> pprint.pformat(cats)
"[{'desc': 'chubby', 'name': 'Zophie'}, {'desc': 'fluffy', 'name': 'Pooka'}]"
>>> fileObj = open('myCats.py', 'w')
>>> fileObj.write('cats = ' + pprint.pformat(cats) + '\n')
83
>>> fileObj.close()