Module 5: Files
Reading andWriting Files:
Files and File Paths, The os.path
Module, The File Reading/Writing
Process, Saving Variables with the
shelve Module.
Reading and Writing files
 Variables are a way to store data while a program is running
 If your data has to exist even after the program has finished
execution , it has to be saved as a file
 Python can be used to create, read, and save files on the hard
drive.
Introduction
File and File Paths
File 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.
C:UsersPublicPicturesSamplePicturesChrysanthemum.jpg
 The part of the filename after the last period is called the file’s
extension and specifies file’s type E.g .jpg
 Users, Public, Pictures and Sample Pictures are names of folders
 Folders can contain files and other folders
 The C: part of the path is the root folder, which contains all other
folders
Backslash onWindows and Forward Slash on
macOS and Linux
 OnWindows, paths are written using backslashes () as the
separator between folder names
 The macOS and Linux operating systems, use the forward
slash (/) as their path separator
 Python scripts should handle both cases
Backslash onWindows and Forward Slash on
macOS and Linux
 If a string of individual file names are passed, path should be
returned using correct separators
 This can be done in two ways
 The Path() function in the pathlib module
 os.path.join() function in the os module
Backslash onWindows and Forward Slash on
macOS and Linux
 The following code joins names from a list of filenames to the
end of a folder’s name
Backslash onWindows and Forward Slash on
macOS and Linux
Using the / Operator to Join Paths
 The / operator can combine
Path objects and strings.
 This is helpful for modifying
a Path object after it is
created
 This is done with the Path()
function.
 Python evaluates the /
operator from left to right
 When using the / operator for joining paths one of the first two
values must be a Path object.
 The / operator replaces the older os.path.join() function
Using the / Operator to Join Paths
The CurrentWorking Directory
 Every program that runs on your computer has a current
working directory, or cwd.
 Folder is the modern term used for directories
 However, current working directory is the standard term in
use
 The current working directory can be obtained with the
Path.cwd() function
 Current working directory can be changed using os.chdir()
from os module
The CurrentWorking Directory
Python will display an error if you try to change to a directory that
does not exist
The CurrentWorking Directory
Absolute vs Relative Paths
 2 ways to specify a file path
 Absolute Path
 Relative Path
 An absolute path, which always begins with the root folder
 A relative path, which is relative to the program’s current working
directory
Absolute vs Relative Paths
 the dot (.)
 dot-dot (..) folders
 A single dot for a folder name is
shorthand for this directory
 Two dots means the parent folder
 In the given example, the cwd is
set to 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 Using the
os.makedirs() Function
 New folders can be created with os.makedirs() function
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.
 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.
HandlingAbsolute and Relative Paths
Getting the Parts of a File Path
 Different parts can be extracted from a file path
 The parts of a file path include the following:
 The anchor, which is the root folder of the filesystem
 OnWindows, the drive, which is the single letter that often denotes a
physical hard drive or other storage device
 The parent, which is the folder that contains the file
 The name of the file, made up of the stem (or base name) and the suffix (or
extension)
 Windows Path objects have a drive attribute, but macOS and Linux Path objects
don’t
Fig: The parts of aWindows (top) and macOS/Linux (bottom) file path
Getting the Parts of a File Path
Getting the Parts of a File Path
os.path.split() does not take a file path and return a list of strings
of each folder.
Contd.,
Finding File Sizes and Folder Contents
 The os.path module provides functions for finding the size of
a file in bytes and the files and folders inside a given folder.
 os.path.getsize(path) will return the size in bytes of the file in
the path argument
Finding File Sizes and Folder Contents
os.listdir(path) will return a list of filename strings for each file in the
path argument.
File
If you want to find the total size of all the files in this directory,
we can use os.path.getsize() and os.listdir() together
Finding Total File Sizes in a Folder Contents
 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.
Checking PathValidity
Checking PathValidity
os.path.exists('h:')
The File Reading/Writing Process
 Plaintext files - contain only basic text characters and do not
include font, size, or color information
 E.g. files with .txt, .py extension which can be opened with Notepad
 Binary files are all other file types, such as word processing
documents, PDFs, images, spreadsheets, and executable programs
 If a binary file is opened in Notepad orTextEdit, it will look like
scrambled text
The File Reading/Writing Process
 There are three steps to reading or writing files in Python:
 Call the open() function to return a File object
 Call the read() or write() method on the File object
 Close the file by calling the close() method on the File object
The File Reading/Writing Process
Opening Files with the open() Function
 The open() function returns a File object
 The path indicating the file to be opened is passed to open()
function
Reading the Contents of Files
 read() function is used to read from a file
Reading the Contents of Files
 readlines() method to get a list of string values from the file,
one string for each line of text
 Except for the last line of the file, each of the string values
ends with a newline character ‘n’
Writing to Files
 You can’t write to a file you’ve opened in read mode
 A file should be opened in write mode or append mode for
writing
 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
Writing to Files
 Write mode will overwrite the existing file and start from
scratch by calling write() on an opened file
 'w' is passed as the second argument to open() to open a file
in write mode
Writing to Files
 Append mode, on the other hand, will append text to the
end of the existing file by calling write() on an opened file
 'a' is passed as the second argument to open() to open a file
in append mode.
SavingVariables with the shelve Module
 Variables in a Python program can be stored to binary shelf
files
 Program can restore data to variables from the hard drive
 shelve module must be imported
 A filename is passed to shelve.open() and pass it, and then
store the returned shelf value in a variable
 When you’re done, call close() on the shelf value
 OnWindows OS three new files in the current working
directory are created: mydata.bak,mydata.dat,and mydata.dir
 These binary files contain the data stored in the shelf
SavingVariables with the shelve Module
SavingVariables with the shelve Module
SavingVariables with the pprint.pformat()
Function
 Using pprint.pformat() will give a string that can be witten to
a .py file.
 This file will be a module that can be imported when required
to use the variable stored in it.
SavingVariables with the pprint.pformat()
Function
SavingVariables with the pprint.pformat()
Function

Module 5_Reading and Writing Files.pptx.

  • 1.
  • 2.
    Reading andWriting Files: Filesand File Paths, The os.path Module, The File Reading/Writing Process, Saving Variables with the shelve Module.
  • 3.
  • 4.
     Variables area way to store data while a program is running  If your data has to exist even after the program has finished execution , it has to be saved as a file  Python can be used to create, read, and save files on the hard drive. Introduction
  • 5.
  • 6.
    File and Filepaths  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. C:UsersPublicPicturesSamplePicturesChrysanthemum.jpg  The part of the filename after the last period is called the file’s extension and specifies file’s type E.g .jpg  Users, Public, Pictures and Sample Pictures are names of folders  Folders can contain files and other folders  The C: part of the path is the root folder, which contains all other folders
  • 7.
    Backslash onWindows andForward Slash on macOS and Linux  OnWindows, paths are written using backslashes () as the separator between folder names  The macOS and Linux operating systems, use the forward slash (/) as their path separator  Python scripts should handle both cases
  • 8.
    Backslash onWindows andForward Slash on macOS and Linux  If a string of individual file names are passed, path should be returned using correct separators  This can be done in two ways  The Path() function in the pathlib module  os.path.join() function in the os module
  • 9.
    Backslash onWindows andForward Slash on macOS and Linux
  • 10.
     The followingcode joins names from a list of filenames to the end of a folder’s name Backslash onWindows and Forward Slash on macOS and Linux
  • 11.
    Using the /Operator to Join Paths  The / operator can combine Path objects and strings.  This is helpful for modifying a Path object after it is created  This is done with the Path() function.  Python evaluates the / operator from left to right
  • 12.
     When usingthe / operator for joining paths one of the first two values must be a Path object.  The / operator replaces the older os.path.join() function Using the / Operator to Join Paths
  • 13.
    The CurrentWorking Directory Every program that runs on your computer has a current working directory, or cwd.  Folder is the modern term used for directories  However, current working directory is the standard term in use  The current working directory can be obtained with the Path.cwd() function
  • 14.
     Current workingdirectory can be changed using os.chdir() from os module The CurrentWorking Directory
  • 15.
    Python will displayan error if you try to change to a directory that does not exist The CurrentWorking Directory
  • 16.
    Absolute vs RelativePaths  2 ways to specify a file path  Absolute Path  Relative Path  An absolute path, which always begins with the root folder  A relative path, which is relative to the program’s current working directory
  • 17.
    Absolute vs RelativePaths  the dot (.)  dot-dot (..) folders  A single dot for a folder name is shorthand for this directory  Two dots means the parent folder  In the given example, the cwd is set to C:bacon The . at the start of a relative path is optional. For example, .spam.txt and spam.txt refer to the same file.
  • 18.
    Creating New FoldersUsing the os.makedirs() Function  New folders can be created with os.makedirs() function
  • 19.
    The os.path moduleprovides 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.  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. HandlingAbsolute and Relative Paths
  • 20.
    Getting the Partsof a File Path  Different parts can be extracted from a file path  The parts of a file path include the following:  The anchor, which is the root folder of the filesystem  OnWindows, the drive, which is the single letter that often denotes a physical hard drive or other storage device  The parent, which is the folder that contains the file  The name of the file, made up of the stem (or base name) and the suffix (or extension)  Windows Path objects have a drive attribute, but macOS and Linux Path objects don’t
  • 21.
    Fig: The partsof aWindows (top) and macOS/Linux (bottom) file path Getting the Parts of a File Path
  • 22.
    Getting the Partsof a File Path
  • 23.
    os.path.split() does nottake a file path and return a list of strings of each folder. Contd.,
  • 24.
    Finding File Sizesand Folder Contents  The os.path module provides functions for finding the size of a file in bytes and the files and folders inside a given folder.  os.path.getsize(path) will return the size in bytes of the file in the path argument
  • 25.
    Finding File Sizesand Folder Contents os.listdir(path) will return a list of filename strings for each file in the path argument.
  • 26.
    File If you wantto find the total size of all the files in this directory, we can use os.path.getsize() and os.listdir() together Finding Total File Sizes in a Folder Contents
  • 27.
     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. Checking PathValidity
  • 28.
  • 29.
  • 30.
     Plaintext files- contain only basic text characters and do not include font, size, or color information  E.g. files with .txt, .py extension which can be opened with Notepad  Binary files are all other file types, such as word processing documents, PDFs, images, spreadsheets, and executable programs  If a binary file is opened in Notepad orTextEdit, it will look like scrambled text The File Reading/Writing Process
  • 31.
     There arethree steps to reading or writing files in Python:  Call the open() function to return a File object  Call the read() or write() method on the File object  Close the file by calling the close() method on the File object The File Reading/Writing Process
  • 32.
    Opening Files withthe open() Function  The open() function returns a File object  The path indicating the file to be opened is passed to open() function
  • 33.
    Reading the Contentsof Files  read() function is used to read from a file
  • 34.
    Reading the Contentsof Files  readlines() method to get a list of string values from the file, one string for each line of text  Except for the last line of the file, each of the string values ends with a newline character ‘n’
  • 35.
    Writing to Files You can’t write to a file you’ve opened in read mode  A file should be opened in write mode or append mode for writing  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
  • 36.
    Writing to Files Write mode will overwrite the existing file and start from scratch by calling write() on an opened file  'w' is passed as the second argument to open() to open a file in write mode
  • 37.
    Writing to Files Append mode, on the other hand, will append text to the end of the existing file by calling write() on an opened file  'a' is passed as the second argument to open() to open a file in append mode.
  • 38.
  • 39.
     Variables ina Python program can be stored to binary shelf files  Program can restore data to variables from the hard drive  shelve module must be imported  A filename is passed to shelve.open() and pass it, and then store the returned shelf value in a variable  When you’re done, call close() on the shelf value  OnWindows OS three new files in the current working directory are created: mydata.bak,mydata.dat,and mydata.dir  These binary files contain the data stored in the shelf SavingVariables with the shelve Module
  • 40.
  • 41.
    SavingVariables with thepprint.pformat() Function
  • 42.
     Using pprint.pformat()will give a string that can be witten to a .py file.  This file will be a module that can be imported when required to use the variable stored in it. SavingVariables with the pprint.pformat() Function
  • 43.
    SavingVariables with thepprint.pformat() Function