pspp-rsk (3)
pspp-rsk (3)
Programming
• Text files
• Binary files
Text files
• A text file is a file that contains printable
characters and whitespace, organized into
lines separated by newline characters. Text
files are structured as a sequence of lines,
where each line includes a sequence of
characters. Since Python is specifically
designed to process text files, it provides
methods that make the job easy.
Binary files
• A binary file is any type of file that is not a
text file. Because of their nature, binary files
can only be processed by an application that
know or understand the file’s structure. In
other words, they must be applications that can
read and interpret binary.
File operation
Open a file
Read or write (perform operation)
Close the file
Attributes of a file
Attribute Description
file.closed If file is closed return true else false
file.mode Returns one of the modes in which the current file is opened
file.name Returns the name of the file
Example for attributes
• fileread= open( “text.txt” , ”r+” )
• print (“Name of the file : ”,fileread.name)
• print (“Closed or not : ”,fileread.closed)
print(“opening mode : ”,fileread.mode)
OUTPUT:
• Name of the file : text.txt
• Closed or not : False
• opening mode : r+
Opening a file
• Python has a built-in function open() to open a file. This
function returns a file object, also called a handle, as it is
used to read or modify the file accordingly.
Syntax:
• Fileobject = open(“filename or file path” ,”access mode”)
• >>> f = open("test.txt") # open file in current directory by
directly specifying the name of the file.
• >>> f = open("C:/Python33/README.txt") # specifying full
path.
• >>> f = open(“test.txt” , “r”) # open the file by mentioning
file name and mode
File opening Modes
Mode Description
r Opens a file for reading only. The file pointer is placed at the beginning of the
file. This is the default mode
w Opens a file for writing only.Overwrites the file if the file exists.If the file does
not exist, creates a new file for writing
a Opens a file for appending. The file pointer is placed at the end of the file. If
the file does not exist, creates a new file for writing
r+ Opens a file for both reading and writing. The file pointer is placed at the
beginning of the file.
W+ Opens a file for both reading and writing. Overwrites the file if the file exists.If
the file does not exist, creates a new file for reading and writing.
A+ Opens a file for both reading and appending. The file pointer is placed at the
end of the file. If the file does not exist, creates a new file for reading and
writing.
File opening Modes
Mode Description
rb Opens a file for both reading in binary format. The file pointer is placed at the
beginning of the file
wb Opens a file for writing only in binary format.Overwrites the file if the file
exists.If the file does not exist, creates a new file for writing
ab Opens a file for appending in binary format. The file pointer is placed at the end
of the file. If the file does not exist, creates a new file for writing
FILE CLOSING
Hello Good
10
morning
We are learn
ing python
Its very interesting
Hello Good morning
We are learning python
Its very interesting
Renaming a file
An existing file can be renamed by using the
method rename()
Syntax:
os.rename(current filename, new filename)
Example:
>>> import os
>>>os.rename(“C:/Python27/myfile.txt”,
“C:/Python27/myfile1.txt”)
Deleting a File
An existing file can be deleted by using the
method remove().
Syntax:
os.remove(filename)
Example:
>>> import os
>>>os.remove(“C:/Python27/myfile1.txt”)
Format Operator
• Definition:
The format operator is used to print the output
in a desired format. The % operator is the
format operator. It is also called as
interpolation operator. The % operator is also
used for modulus. The usage varies according
to the operands of the operator.
General syntax:
<format expression>%(values)
# The values is a tuple with exactly the number
of items specified by the format expression.
PROGRAM
print(“%d” %(45))
print(“%f”%(3.14159265358979))
print(“%0.2f”%(3.141592653589))
print (“Decimal : %u”%(24))
Print (“Octal : %o”%(24)) Print (“Hexadecimal :
%x”%(1254))
Print (“Hexadecimal : %X”%(1254))
X=40
print(“The given integer is %d”%X)
OUTPUT
45
3.141592
3.14
Decimal : 24
Octal : 30
Hexadecimal : 4e6
Hexadecimal : 4E6