File System

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

FILE HANDLING

1) The programs that we have done till now executes and gives output. But these outputs can not
be stored for later review because, the variables used had a life till execution. To store and
review these outputs, file handling is must.
2) To avoid repetitive tasks, save time, and save outputs we now study file handling.
3) A file is a named location on a secondary storage media where data are permanently stored for
later access.
4) Data files are those files that store data pertaining to some application.
5) Text files contain only the ASCII equivalent of the contents of the file whereas a .docx file
contains many additional information. That’s why .txt has few bytes but .docx or .docs has few
kilobytes.

Text files Binary files


1. consists of human readable characters 1. made up of non-human readable
2. opened by any text editor characters and symbols
3. a sequence of characters 2. require specific software for reading
4. .txt, .py, .csv 3. represent the actual content such as
5. Contents are stored in binary format, image, audio, video
when opened translation takes place at 4. stored in bytes.
each step from binary to Unicode value 5. Even a single change in byte sequence
to a character corrupts the file
6. Each line is terminated by EOL. 6. When opened with text editor, it shows
garbage values

6) The I/O module in python helps in opening, closing and managing data files. It also has special
functions.
7) open() – this function is used to open files in python. it returns the values stored in a file through
a file object. If file doesn’t exist, it creates a new file and names so.
8) The file_object establishes a link between the program and the data file stored in the permanent
storage. The attributes of a file_object are
a. <file.closed> - returns true if the file is closed and false otherwise.
b. <file.mode> - returns the mode of file
c. <file.name> - returns the name of the file
9) The syntax of open() is file_object= open(file_name, access_mode). The file name should be in
current directory else, path must be specified. The access_mode is an optional argument.
10) The access mode refers to mode in which the file would be accessed. It is also known as
processing mode. By default, files are opened in r mode.

Page 1 of 4
11) close() – this function is used to close files in python. it frees the memory allocated to the file
object. The syntax is file_object.close(). If the file object is re-assigned to some other file, the
previous file is automatically closed.
12) Python makes sure that any unwritten or unsaved data is flushed off (written) to the file before it
is closed. Hence, it is always advised to close the file once our work is done.
13) To open a file using with clause - the advantage of is that any file opened is closed automatically,
once the control comes outside the with clause. The syntax is with open (file_name,
access_mode) as file_ object:
14) For writing a file we can open it in two ways – one is in append mode when we want to add
something new at the end, the other is in write mode when we want to overwrite contents. Two
ways to write in a text file are as follows –

write() writelines()
1. For writing a single string 1. For writing multi line strings
2. Accepts the string as an argument and 2. Doesn’t return the number of strings
writes it. written
3. It returns the number of characters 3. Strings are written in any sequence
written data type form and written.
4. To write numeric data, we need to
convert to a string.
5. Writes into a buffer, when file is closed,
it pushes to permanent storage

15) To read a file, we can open in anyone of the following modes – r, r+, w+, a+

read() readline() readlines()


1. Read specified number of 1. Reads a line and can also be used for 1. reads all the lines and
bytes specific bytes maximum upto \n returns the lines along with
2. file_object.read(n) 2. If no argument or a negative newline as a list of strings.
3. If no argument or a negative number is specified, it reads a 2. lines in the file become
number is specified in read(), complete line and returns string. members of a list, where
the entire file content is read. 3. It returns an empty string when each list element ends with
EOF is reached a newline character (‘\n’).

Page 2 of 4
16) The file offset position in the table refers to the position of the file object when the file is opened
in a particular mode.

tell() seek()
1. returns an integer that specifies the 1. used to position the file object at a
current position of the file object particular position
2. file_object.tell() 2. file_object.seek(offset [,
reference_point])
3. offset can be 0,1,2 only.
4. by default offset is zero

BINARY FILES

1) Serialization is the process of transforming data or an object in memory (RAM) to a stream of


bytes called byte streams.
2) These byte streams in a binary file can then be stored in a disk or in a database or sent through a
network. Serialization process is also called pickling.
3) De-serialization or unpickling is the inverse of pickling process where a byte stream is converted
back to Python object.
4) The pickle module deals with binary files. Here, data are not written but dumped and similarly,
data are not read but loaded.

dump() load()
1. Pickling 1. unpickling
2. To write into a binary file 2. to read from a binary file
3. dump(data_object, file_object) 3. Store_object = load(file_object)

CSV FILES

1) a simple flat file in a human readable format which is extensively used to store tabular data, in a
spreadsheet or database.
2) A CSV file stores tabular data (numbers and text) in plain text.
3) Each line in a file is known as data/record. Each record consists of one or more fields, separated
by commas (also known as delimiters), i.e., each of the records is also a part of this file. Tabular
data is stored as text in a CSV file.
4) The several advantages that are offered by CSV files are as follows:
• faster to handle.
• smaller in size.
• easy to generate and import onto a spreadsheet or database.
• human readable and easy to edit manually.
• simple to implement and parse.
• processed by almost all existing applications.
5) To perform operations on a csv file, csv module is first imported.
6) To read contents of a csv file,
7) reader() – every record is read and stored in form of list. next() method returns the current row
and advances the iterator to the next row.
8) To write to a CSV file in Python, we can use the csv.writer() function. The csv.writer() function
returns a writer object that converts the user’s data into a delimited string.
9) This string can later be used to write into CSV files using the writerow() function.

Page 3 of 4
10) In order to write to a CSV file, we create a special type of object to write to the CSV file “writer
object”, which is defined in the CSV module, and which we create using the writer() function.
11) The writerow() method allows us to write a list of fields to the file. The fields can be strings or
numbers or both.
12) Also, while using writerow(), you do not need to add a new line character (or other EOL
indicator) to indicate the end of the line; writerow() does it for you as necessary

Page 4 of 4

You might also like