0% found this document useful (0 votes)
2 views10 pages

File Handling

The document provides an overview of file handling in Python, including definitions, types of files (text and binary), and commands for opening, reading, writing, seeking, flushing, and closing files. It details the syntax for various commands and the importance of using the 'with' statement for file operations. Additionally, it explains the difference between relative and absolute paths and includes shortcuts for reading files.

Uploaded by

Aarna Bhura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

File Handling

The document provides an overview of file handling in Python, including definitions, types of files (text and binary), and commands for opening, reading, writing, seeking, flushing, and closing files. It details the syntax for various commands and the importance of using the 'with' statement for file operations. Additionally, it explains the difference between relative and absolute paths and includes shortcuts for reading files.

Uploaded by

Aarna Bhura
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

1.

Contents
1. Contents.....................................................................................................1
1. Definitions:.................................................................................................2
2. Types of files:.............................................................................................2
2.1 Text Files – Stores information in the form of a stream of ASCII Code.. 2
2.1.1 Regular text files – Normal text files....................................................2
2.1.2 – Tab Separated values (TSV) – Tab separated values are stored in the
file.................................................................................................................2
2.1.3 – Comma Separated Values (CSV) – Comma separated values are
stored in file..................................................................................................2
2.2 Binary File – A binary file stores the information in the form of a
stream of bytes.............................................................................................2
3. Open Command.........................................................................................2
4. Read Commands........................................................................................4
4. Write Commands........................................................................................4
5. Seek Command..........................................................................................4
6. Flush Command - fd.flush()........................................................................4
7. Close Command - fd.close().......................................................................4
8. With command syntax and usage..............................................................5
2. Short Cuts to reading files..........................................................................6
File Handling
1. Definitions:
1 Relative and Absolute Paths.
i. Relative path – The file name mentions the path relative to
the current working directory.
ii. Absolute Path – The file name mentions the complete path
from the top-level directory.
2 File Object – A file object is a reference to a file on disk. It opens
and makes it available for a number different task.
3 Offset - The position where the file reader is currently pointing.
2. Types of files:
2.1 Text Files
Stores information in the form of a stream of ASCII Code.
2.1.1 Regular text files

Normal text files.

2.1.2 Tab Separated values (TSV)

Tab separated values are stored in the file.

2.1.3 Comma Separated Values (CSV)

Comma separated values are stored in file.

2.2 Binary File

A binary file stores the information in the form of a stream of bytes.

3. Open Command
fd = open (file_name,mode)
open() Parameters

 file - path-like object (representing a file system path)


- The file_name should have \\ in file name.
fd= open(“C:\\Prog\\text1.txt” , “r”)
- In case the file name is mentioned with \\ add a ‘r’ in front of file
name.
fd= open(r“C:\Prog\text1.txt” , “r”)
- IN case only file name is mentioned, it is opened at the location
where python is run.

 mode (optional) - mode while opening a file. If not provided, it defaults


to 'r' (open for reading in text mode). Available file modes are:
Mod Description
e
'r' Open a file for reading. (default)(goes to first n rreads)
'w' Open a file for writing.
Creates a new file if it does not exist or truncates the file if it exists( goes to
first , overwrites).
'x' Open a file for exclusive creation.
If the file already exists, the operation fails.
'a' Open for appending at the end of the file without truncating it.
Creates a new file if it does not exist.( goes to last , uske neeche likh)
't' Open in text mode. (default)
'b' Open in binary mode.
'+' Open a file for updating (reading and writing)

Return Value from open()

The open() function returns a file object which can used to read, write
and modify the file.
If the file is not found, it raises the FileNotFoundError exception.
4. Read Commands
1 read() : This function reads the entire file and returns a string
2 read(n) : This function reads n bytes from the file and returns a
string
3 readline() : This function reads lines from that file and returns
as a string. It will fetch the line n, if it is been called nth time.
4 readlines() : This function returns a list where each element is
single line of that file.

Returns an empty string when end of file is encountered. There is


no error in this.

4. Write Commands
a. write() : This function writes a fixed sequence of characters
to a file.
b. writelines() : This function writes a list of string.

5. Seek Command
fd.seek(offset, reference)
- Offset : no of bytes.
- Reference – 0: Beginning of the file.
1: Current position of the file.
2: End of the file.

Negative offset does not work with text files.


6. Flush Command - fd.flush()
Flushes the write buffer in the file.
7. Close Command - fd.close()
Closes the file.
Why file closing is important?
1. It is a good programming practice.
2. To ensure that data buffer has been written on the file.
3. To make sure operating system does not get stuck and
blocked memory is freed.
8. With command syntax and usage.
Syntax – with open (<file_name> , <filemode>) as <filehandle> :
<File manipulation statements>
Example:
With open(“Sample.txt”, “r”) as fd :
X=fd.read()
print(X)

- With statement closes the file by itself.


- All the commands related to the file as to be present in the with
block.
9. Short Cuts to reading files
1. X = open(“C:\\Prog\\text1.txt” , “r”).read()
Print (X)
2. fd = open(“C:\\Prog\\text1.txt” , “r”)
for line in fd
print(line)

in both the cases contents of the file would be printed.


File name is relative ( someone asks be t blok I assume green apk…)
It along with entire locaton c//… is absolute
// zaroori python doesn’t take /

Fd is file descriptor

Read mode- file not present eroor , write mode , append will make
file if its nt there
Note :
X=fd.read() reads entir line
X=fd.read(10) reads 10 bytes
C=fd.readline(0 gives abc/n……
X=fd.readlines() reads everyting

TELL- tells where cursor is while reading


SEEK-

Offset tells kitne aage jana hai , ref tells kahan se jana hai
Ref-0=beg of file , 1 =surrent post , 2= end off file use din csu and
binary files not text files

1. r + means resd mode , it also means jo function khol rakha hai


use jyada karna hai so we can write also in this mode
2. while writing write function we have ti put \n because it does not
automatically go to next line
3. ASK ABOUT FD.FLUSH

You might also like