Python Files
Python Files
• We have experience creating files, such as the python program file, using a
word processor. From this experience, we realize that a file is simply a
sequence of characters stored on our computer or network.
• One of the things that make a file different from a string (or) list of
characters is that the file exists even after a program ends.
• This makes a file useful for maintaining information that must be
remembered for a long period of time such as the persistent programs.
• Most of the programs we have seen so far are transient in the sense that
they run for a short time and produce some output, but when they end
their data disappears. If we run the program again, it starts with a clean
slate.
• Other programs are persistent, they run for a long time (or all the time);
they keep at least some of their data in permanent storage (a hard drive
for example); and if they shutdown and restart, they pick up where they
left off.
• Eg: Operating system which run pretty much whenever a computer is on,
and web servers, which run all the time, waiting for requests to come in
on the network.
What is a File?
• File is a named location on disk to store related information. It is used to
permanently store data in a non-volatile memory (e.g: hard disk).
• Since, random access memory (RAM) is volatile which loses its data when
computer is turned off, we use files for further use of the data.
TEXT FILE:
• A Text File is a file containing characters, structured as individual lines of
text.
• In addition to printable characters, text files also contain the non-printable
new line character (\n), to denote the end of each text line.
• The newline character causes the screen cursor to move to the beginning
of the next screen line.
• Text files can be directly viewed and created using a text editor.
What is a File?
BINARY FILE:
• Binary files can contain various types of data, such as numerical values,
and are therefore not structured as lines of text.
• These files can only be read and written via a computer program.
• Any attempt to directly view a binary file will result in “garbled” characters
on the screen.
FILE SYSTEMS
• Our computer drive is organized in a hierarchical structure of files and
directories
Files:
• These contain information.
• Example: .csv files or .py(python) files
Directories:
• Theses contain files and directories.
• Files system starts from a root directory, notated by a forward slash / on
Unix and by a drive letter c:/ on windows.
Absolute and Relative File Path
• A path is a unique location to a file or a folder in a file system of an OS. A
path to a file is a combination of / and alpha numeric characters
Absolute path_name:
• It is defined as specifying the location of a file (or) directory from the root
directory (/).
• To write an absolute path name
– We should start at the root directory and work down
– Write a slash (/) after every directory name
– Eg: /home/example_user/example_directory (or) c:/system32/cmd.exe
• An absolute file path describes how to access the given file or directory,
starting from the root of the file system.
• A file path is also called a pathname
Absolute and Relative File Path
Relative file path:
• It is defined as the path related to the present working directory. It starts
at our current directory.
– Eg: example_directory
• A relative file path is interpreted from the perspective of our current
directory. If we use a relative file path from the wrong directory, then the
path will refer to a different file than we intend (or) it will refer to no file at
all.
• In a sense, whenever we use a relative file path, it is joined with out
current directory to create an absolute file path.
• If our current working directory is
– home/example_user then we use a relative file path as
example_directory/example_python_program, then that is equivalent to using the
absolute file path.
– /home/example_user/example_directory/example_python_program
Opening Text Files
• All files must be opened before they can be read from (or) written to.
• In python, when a file is successfully opened, a file object is created that
provides methods for accessing the files.
• We look how to open files for reading (or) writing to a file.
• Python has a built-in function open() to open a file
• The open() function opens a file and returns it as a file object with that file
object, we can create update, read, and delete files.
– Open(file, mode, buffering, encoding, errors, newline, closefd, opener)
Opening Text Files
Parameter Condition Description
file Required The path and name of the file
mode Optional Specified the mode we want to open the file
buffering Optional Sets the buffering policy
encoding Optional Specifies encoding
error Optional Specifies different error handling scheme
newline Optional Controls how universal newline mode works
closefd Optional Keeps the underlying file descriptor open
when the file is closed
opener Optional A curtain opener used for low level
operations
Open a File
• We can open a file using open() built-in function specifying its name.
– f = open(‘file.txt’)
• When we specify the filename only, it is assumed that the file is located in
the same folder as python.
• We can also specify the exact path that the file is located at specified
absolute path.
– F = open(c:\pyton33\scripts\myfile.txt)
• When specifying the exact path, characters preferred by \ (like \n \r \t etc)
are interpreted as special characters.
• We can escape them using double slashes.
– ‘c:\\new\\text.txt’
Specify File Mode
• Here are 5 different modes we can use to open the file.
Character Mode Description
‘r’ Read (Default) Open a file for read only.
‘w’ Write Open a file for write only (Overwrite).
‘a’ Append Open a file for write only (append).
‘r+’ Read + Write Open a file for both reading and writing.
‘x’ Create Create a new file.
Specify Encoding
• By specifying <encoding> parameter, we can decode or encode the file in
popular encoding like ‘ascii’, ‘UTF-8’, etc.
• Read a file in ‘UTF-8’ Encoding
– f = open(‘myfile.txt’, encoding = ‘UTF-8’)
• Read a file in ‘ascii’ Encoding
– f = open(‘myfile.txt’, encoding = ‘ascii’)
Handling Encoding and Decoding Errors
• By default, python raises Unicode Error exception encoding (or) decoding
error.
• However, we can specify how these errors are to be handled using
<errors> parameter.
• File contains:
Das Straβe
• Code:
f = open('myfile.txt',encoding='ascii',errors='ignore')
print(f.read())
• Output:
Das Strae
Handling Encoding and Decoding Errors
• Unlike other languages, the character ‘a’ does not imply the number 97
until it is encoded using ASCII (or other equivalent encodings).
• Moreover, the default encoding is platform dependent. In windows, it is
‘CP1252’ but ‘UTF-8’ in Linux
• So, we must not also rely on the default encoding or else our code will
behave differently in different platforms.
• Hence, when working with files in text mode, it is highly recommended to
specify the encoding type.
f = open('myfile.txt',mode='r',encoding='utf-8')
print(f.read())
OUTPUT:
Das Straβe
How to close a file using PYTHON
• When we are done with operations to the file, we need to properly close
the file.
• Closing a file will free up the resources that were tied with the file and is
done using python close() method
• Python has a garbage collector to clean up unreferenced objects but, we
must not rely on it to close the file.
f = open('myfile.txt',encoding='utf-8')
print(f.read())
f.close()
OUTPUT:
Das Straβe
• This method is not entirely safe, if an exception occurs when we are
performing some operation with the file, the code exits closing the file.
How to close a file using PYTHON
• A safer way is to use a try…..finally block.
try:
f = open('myfile.txt',encoding='utf-8')
print(f.read())
finally:
f.close()
OUTPUT:
Das Straβe
• This way we are guaranteed that the file is properly closed even if an
exception is raised causing program flow to stop.
• This ensures that the file is closed when the block inside with is exited.
• We don’t need to explicitly call the close() method, it is done internally.
with open('myfile.txt',encoding='utf-8') as f:
print(f.read())
OUTPUT:
Das Straβe
How to write a file using PYTHON
• In order to write into a file in python, we need to open it in write ‘w’,
append ‘a’ or exclusive creation ‘x’ mode.
• We need to be careful with the ‘w’ mode as it will overwrite into the file if
it already exists. All previous data are erased.
• Writing a string or sequence of bytes (for binary files) is done using write()
method. This method returns the number of characters written to the file.
• To write, first we need to open the file in one of the writing modes (‘w’,’a’
or ‘r+’)
How to write a file using PYTHON
‘w’ overwrites entire file content
textfile.txt
f=open('textfile.txt','w')
f.write('Python is an interpreted, high-level, general-purpose
programming language. Created by Guido van Rossum and first
released in 1991')
f.close()
textfile.txt
textfile.txt
f=open('textfile.txt',’a')
f.write(‘Python allows programming in Object-Oriented and
Procedural paradigms')
f.close()
textfile.txt
f=open('textfile.txt',’r+')
f.write(‘C is a Procedural Programming Language ')
f.close()
textfile.txt
f=open('textfile.txt','w')
lines=['New line1\n','New line2','New line3']
f.writelines(lines)
f.close()
textfile.txt
New line1
New line2New line3
Flush Output Buffer
• When we write to a file, the data is not immediately written to the disk
instead it is stored in the buffer memory
• It is written to the disk only when we close the file or manually flush the
output buffer
textfile.txt textfile.txt
f=open('textfile.txt','a') f=open('textfile.txt','a')
f.write('Last line in the file') f.write('Last line in the file')
f.flush()
textfile.txt textfile.txt
• However, if we try to open a file (that does not exist) for writing using w, a,
or r+ mode, python will automatically create the file for us.
Delete a File
• We can delete a file by importing the OS module and using its remove()
method.
import os
os.remove('textfile.txt')
Check if File Exists
• An error will occur if we try to open or delete a file that does not exist.
import os
os.remove('textfile.txt')
• To check if file exists without getting an error; we can use isfile() method
from OS module.
import os
OUTPUT:
if os.path.isfile('texfile.txt'):
f=open('textfile.txt')
The file does not exist
else:
print('The file does not exist')
How to read files in python
• To read a file in python, we must open the file in reading mode.
• To read its contents, we can use read() method.
• By default, the read() method reads the entire file
textfile.txt OUTPUT:
f=open('textfile.txt')
First line of the file First line of the file
print(f.read())
Second line of the file Second line of the file
Third line of the file Third line of the file
textfile.txt OUTPUT:
textfile.txt OUTPUT:
First line of the file f=open('textfile.txt') First line of the file
Second line of the file for line in f: Second line of the file
Third line of the file print(line, end="") Third line of the file
• If we want to read all the lines in a file into a list of strings we use
readlines() method
OUTPUT:
f=open('textfile.txt')
print(f.readlines()) ['First line of the file\n', 'Second line of
the file\n', 'Third line of the file']
f=open('text1.txt','w')
f.write('''Computer is an electronic machine
that accepts raw data and process
them based on set of intructions
given to it to produce information''')
f.flush()
f.close()
f=open('text1.txt','rt')
for l in f:
print(l,end="")
f.close()
text1.txt
alpha.txt
ABCDEFGHIJKLMNOPQRSTUVWXYZ
f=open('alpha.txt','rb')
f=open('alpha.txt','rb')
print(f.read(2)) #b'AB'
print(f.tell()) #0
f.seek(6)
f.read(5)
x=f.read(4)
print(f.tell()) #5
print(x) #b'GHIJ'
f.seek(-3,2)
f.seek(5,1)
f.read(2)
print(f.read(1)) #b'P'
print(f.tell()) #25
f.seek(-3,2)
print(f.read(2)) #b'XY'
• If we want to check the current position of the file pointer, use the tell()
method
File Object Attributes
• Once a file is opened, we have one file object. From this we can get
various information related to that file
– file.closed – returns true, if file is closed, false otherwise
– file.mode – returns access mode with which file was opened
– file.name – returns name of the file
f=open('alpha.txt','rt') OUTPUT:
print("File Closed: ",f.closed)
print("Mode: ",f.mode) File Closed: False
print("File name: ",f.name) Mode: rt
f.close() File name: alpha.txt
print("File closed: ",f.closed) File closed: True
What is Directory in Python?
• If there are a large number of files to handle in our python program, we
can arrange our code within different directories to make things more
manageable.
• A directory or folder is a collection of files and sub directories.
• Python has the OS module, which provides us with many useful methods
to work with directories (and files as well)
Get current Directory
• We can get the present working directory using the getcwd() method
• This method returns the current working directory in the form of a string.
• We can also use the getcwdb() method to get it as bytes object.
>>> import os
>>> os.getcwd()
'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python37-32'
>>> os.getcwdb()
b'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python37-32'
• The extra backslash implies escape sequence. The print() function will
render this properly.
>>> import os
>>> print(os.getcwd())
C:\Users\Admin\AppData\Local\Programs\Python\Python37-32
>>> print(os.getcwdb())
b'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python37-32'
Changing Directory
• We can change the current working directory using the chdir() method.
• The new path that we want to change to must be supplied as a strong to
this method.
• We can use both forward slash(/) or the backward slash (\) to separate
path elements.
• It is safe to use escape sequence when using the backward slash
>>> import os
>>> os.chdir("c:\\Users")
>>> print(os.getcwd())
c:\Users
List Directories and Files
• All files and sub directories inside a directory can be known using the
listdir() method.
• This method takes in a path and returns a list of sub directories and files in
that path.
• If no path is specified, it returns from the current working directory.
>>> print(os.getcwd())
c:\Users
>>> os.listdir()
['Admin', 'Administrator', 'All Users', 'Default', 'Default User', 'desktop.ini',
'Public']
Making a New Directory
• We can make a new directory using mkdir() method.
• This method takes in the path of the new directory.
• If the full path is not specified, the new directory is created in the current
working directory.
>>> os.mkdir(“c:\\Test”)
>>> os.mkdir(‘Test’)
>>> os.rename(‘Test’,’Test2’)
Removing Directory or File
• A file can be removed (deleted) using the remove() method.
• Similarly, the rmdir() method removes an empty directory.
– Eg: >>> os.rmdir(‘TT’)
• rmdir() method can only remove empty directories.
• In order to remove a non-empty directory we can use the rmtree()
method inside the shutil module.