Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
27 views

Module - 3

The document discusses reading and writing files in Python. It covers key file concepts like filenames, paths, extensions, directories and folder organization. It describes functions in the os and os.path modules for working with files and paths, including joining paths, getting/changing the current working directory, checking path validity, getting file sizes and listing folder contents. It also covers the three step process of opening a file, reading/writing from it, and closing it using the open(), read()/write() and close() functions.

Uploaded by

bldeepak2319
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

Module - 3

The document discusses reading and writing files in Python. It covers key file concepts like filenames, paths, extensions, directories and folder organization. It describes functions in the os and os.path modules for working with files and paths, including joining paths, getting/changing the current working directory, checking path validity, getting file sizes and listing folder contents. It also covers the three step process of opening a file, reading/writing from it, and closing it using the open(), read()/write() and close() functions.

Uploaded by

bldeepak2319
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

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 projects.docx 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. 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:

>>> myFiles = ['accounts.txt', 'details.csv', 'invite.docx']

>>> for filename in myFiles:

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.

• 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, .\spam.txt and spam.txt refer to the same file.
Creating New Folders with os.makedirs()

• 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.abspath(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 os.path.isabs(path) will return True if the argument is an


absolute path and False if it is a relative path.
Cont..

• 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.

Try these functions in the interactive shell:


>>> os.path.abspath('.')
'C:\\Python34'
>>> os.path.abspath('.\\Scripts')
'C:\\Python34\\Scripts'
>>> os.path.isabs('.')
False
>>> os.path.isabs(os.path.abspath('.'))
True
Cont..
• Enter the following calls to os.path.relpath() into the interactive shell:
>>> os.path.relpath('C:\\Windows', 'C:\\')
'Windows'
>>> os.path.relpath('C:\\Windows', 'C:\\spam\\eggs')
'..\\..\\Windows'
>>> os.getcwd()
'C:\\Python34‘

• Calling os.path.dirname(path) will return a string of everything that comes


before the last slash in the path argument. Calling os.path.basename(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:
>>> calcFilePath.split(os.path.sep)
['C:', 'Windows', 'System32', 'calc.exe']
On OS X and Linux systems, there will be a blank string at the start of
the returned list:
>>> '/usr/bin'.split(os.path.sep)
['', '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 os.path.sep.
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 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.

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 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:

>>> helloFile = open('C:\\Users\\your_home_folder\\hello.txt')

If you’re using OS X, enter the following into the interactive shell instead:

>>> helloFile = open('/Users/your_home_folder/hello.txt')


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/hello.txt', 'r') and open('/Users/asweigart/


hello.txt') 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\hello.txt", "r")

print(f.read())

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 os.remove()
function:

>>import os
>>os.remove(“C:\\Users\\win8\\Desktop\\coding\hello2.txt")

Check if file exists, then delete it:

import os
if os.path.exists("demofile.txt"):
os.remove("demofile.txt")
else:
print("The file does not exist")
Delete Folder

• To delete an entire folder, use the os.rmdir() method:

>>import os
>>os.rmdir("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 = shelve.open('mydata')
>>> cats = ['Zophie', 'Pooka', 'Simon']
>>> shelfFile['cats'] = cats
>>> shelfFile.close()

>>> shelfFile = shelve.open('mydata')


>>> list(shelfFile.keys())
['cats']
>>> list(shelfFile.values())
[['Zophie', 'Pooka', 'Simon']]
>>> shelfFile.close()
Saving Variables with the pprint.pformat() Function

Recall from “Pretty Printing” that the pprint.pprint() function will


“pretty print” the contents of a list or dictionary to the screen, while the
pprint.pformat() function will return this same text as a string instead of
printing it.

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()

You might also like