File Handling in Python

File: In Python, a file is a named area on disk or in memory used to store data permanently. A file can be used to store text, binary data, or other types of data. Python provides built-in functions and methods for working with files, which allows you to open, create, read, write, append, modify and close files.

Data Files: These files store the data required for a specific application.

Types of Files:
1) Text Files
2) Binary Files
3) Delimited Text Files (CSV or TSV Files)

Text Files: In Python, a text file is a file that contains human-readable text. Text files are usually created with a text editor, and they can be read and written in Python using the built-in file handling functions.
1) They store information in the form of ASCII/Unicode characters.
2) Each line of text is terminated or delimited with a special character known as EOL (End of Line).
3) Some internal translations take place when EOL character is read or written.
4) In Python, default EOL character is ‘\n’ or ‘\r\n’
5) They store the text in same form as typed, and have extension .txt
6) They are also known as regular text files.


Regular text file

Delimited Text Files: In these files, a specific character is used to separate the values. After each value, a specific character like a comma or a tab is stored. It is of two types
1) CSV Files
2) TSV Files

CSV Files: CSV Files stores a tabular data that has been saved as a plaintext, where data is separated by commas. They have the extension .csv and follow two rules:
1) Number of rows in CSV File = Number of rows in table
2) Comma , appears after every field (except the last field)
Advantages of CSV Files:
1) It is simple and compact format for data storage.
2) It is a common format for data interchange.
3) It can be opened in any spreadsheet application software package like MS-Excel, calc etc.
4) All spreadsheets and databases support import and export to csv files.
5) Commonly used delimiter is comma, but we can also use any other character like $,# etc. as separator character to separate different values.
6) It is easier to create csv file
7) CSV files are capable of storing large amount of data

TSV File: they are similar to CSV files but, In these files, a tab (\t) is used to separate different values.

RollNo         Name           Marks
1              Sheetal        500
2              Amit           400
3              Nidhi          300
4              Rajesh         450
5              Ansh           350
                 TSV File 

                   CSV File

Binary Files: In Python, a binary file is a file that contains textual and non-textual data, such as images, audio files, video files, and executable programs. Binary files are usually opened in binary mode to avoid any character encoding issues, and they can be read and written using the built-in file handling functions.
1) It stores information in the form of a stream of bytes
2) It contains the information in the same format in which information is held in memory.
3)There are no delimiters for a line and thus no translation for EOL occurs, So it is faster to read and write binary files.
4) It is not in human readable form.
5) They can have any extension

File Operations : We can perform various operations on a file such as
1) Opening the already existing file
2) Creating the new file
2) Writing data into file
3) Reading data from the file
4) Appending data into the file
5) Searching data into the file
6) Updating data into the file
7) Closing the file

File Object: It is a built-in object or link used to interact with a file on disk or in memory. A file object is created using the open() function, and provides methods and attributes for reading and writing data to the file. All operations are performed on files through the file object or file handle. To create a file object in Python, you can use the open() function, which takes two arguments: the name of the file, and the mode in which the file should be opened.

Opening Files: We can open files using the built-in function open(). The open() function takes two arguments: the name of the file you want to open, and the mode in which you want to open the file. Syntax is
file_object_name = open(“file name with complete path” , “file mode”)

File Mode: The file mode determines the purpose of opening the file i.e. whether the file is opened for reading, writing, appending, or some combination of these operations. The file mode is specified as the second argument to the open() function, and it is a string that contains one or more characters.
The most common file modes are:

  1. ‘r’ open for reading (default)
  2. ‘w’ open for writing, truncating the file first
  3. ‘x’ create a new file and open it for writing
  4. ‘a’ open for writing, appending to the end of the file if it exists
  5. ‘b’ binary mode
  6. ‘t’ text mode (default)
  7. ‘+’ open a disk file for updating (reading and writing)
  8. ‘U’ universal newline mode (deprecated)

1) Default file opening mode is “rt” which opens a text file for reading if file exists and gives error if file doesn’t exist.
2) In write mode, file is created if it doesn’t exist and overwritten if file already exists while “X” mode (exclusive creation) is used to create a new file and open it for writing. If the file already exists, the operation will fail and raise a FileExistsError. This mode is useful when you want to create a new file and avoid accidentally overwriting an existing file.
3) In append mode, file is created if it doesn’t exist and opened (but not ovrewritten) if file already exists and new contents are appended at the end of the file.
4) While opening files in read and write file modes, file pointer is placed at the begining of the file but in append mode, file pointer is placed at the end of the file.
5) We can write exactly any one of create/read/write/append mode i.e r/w/a/x mode at a time
6) We can’t have text and binary mode at the same time
7) ‘U’ mode is deprecated and will raise an exception in future versions of Python. It has no effect in Python 3.
8) We can combine any two or more possible combinations of file modes in any possible way by writing as
a) ‘rb’ , ‘br’ , ‘rt’ , ‘tr’ , ‘r+b’ , ‘rb+’ , ‘+rb’ , ‘b+r’ , ‘br+’ , ‘+br’
b) ‘wb’ , ‘bw’ , ‘wt’ , ‘tw’ , ‘w+b’ , ‘wb+’ , ‘+wb’ , ‘b+w’ , ‘bw+’ , ‘+bw’
c) ‘ab’ , ‘ba’ , ‘at’ , ‘ta’ , ‘a+b’ , ‘ab+’ , ‘+ab’ , ‘b+a’ , ‘ba+’ , ‘+ba’
d) ‘xb’ , ‘bx’ , ‘xt’ , ‘tx’ , ‘x+b’ , ‘xb+’ , ‘+xb’ , ‘b+x’ , ‘bx+’ , ‘+bx’

>>># default mode is "rt" i.e. open a text file for reading
>>> f1=open("garg.txt")

>>>#opening a text file for reading
>>> f2=open("garg.txt" ,"r")

>>>#opening a text file for writing
>>> f3=open("garg.txt" ,"w")

>>>#opening a text file for appending
>>> f4=open("garg.txt" ,"a")

>>>#will give error because file already exists, and it is used to avoid accidentally overwriting it.
>>> f5=open("garg.txt" ,"x")
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    f5=open("garg.txt" ,"x")
FileExistsError: [Errno 17] File exists: 'garg.txt'

>>>#error because file is opened for reading (default mode) and file doesn't exist
>>> f6=open("gupta.txt")
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
FileNotFoundError: [Errno 2] No such file or directory: 'gupta.txt'

>>># We must give double slashes as it is a escape sequence and special meaning attached to \ character
>>> f7=open("f:\\demo\\file1.txt")

>>># We can write r to specify raw string in which single slashes are used
>>># which means no special meaning attached to any character
>>> f8=open(r"f:\demo\file1.txt")

>>># no space allowed between r and file name
>>> f9=open(r "f:\demo\file1.txt")
SyntaxError: invalid syntax

>>># opens a binary file in write mode but both reading and writing operations can be performed
>>> f10=open("f:\\demo\\file1.dat", "+wb")

To avoid/handle FileNotFoundError Exceptions (as in above case of opening file gupta.txt and assigning the link to file pointer f6) , we must always open a file in read mode in try and except blocks. In this case, our program will not interrupt, but gives proper error message like “File not Found” or any other message specified by the user.

>>> try:
	print("File not Fund")

File not Fund

Path: It is a sequence of directory names which give you the hierarchy to access a particular directory of file.
Path Name: It is the full name of a file or a directory.
Absolute Path: It is the complete path that starts from the root directory and includes all directories and subdirectories necessary to locate the file. Example: F:\demo\file1.txt
Relative paths: It is a path that is relative to the current working directory CWD and does not start from the root directory. It describes the path to a file or directory in relation to the current working directory denoted by a . (dot) and its parent directory denoted with .. (two dots). It is used to locate files that are stored in the same directory as the current working directory, or in subdirectories. Your CWD is where you have opened the file in which you are running the python shell or where you are writing the python code. Suppose your CWD is C:\python
1) .file1.txt will search in CWD i.e. C:\python\file1.txt
2) ..file1.txt will search in parent folder of CWD i.e. C:\file1.txt
3) .demo\file1.txt will search in CWD and then find the file named file1.txt in the folder named demo i.e. C:\python\demo\file1.txt
4) ..demo\file1.txt will search in parent folder of CWD and then find the file named file1.txt in the folder named demo i.e. C:\demo\file1.txt

Closing Files: Once you are done reading from the file or performing other operations on it, you can use the close() method to close the file. It’s generally a good practice to close files , as leaving files open can cause problems with file locking, resource usage, and potential data corruption. In Python, files are automatically closed when the program terminates, but it’s still a good idea to close them explicitly to avoid any issues. We can close a file using the close() method. Syntax is:
Example: f1.close()
It breaks the link of the file object and the file on the disk/memory. After that no task can be performed on that file.

Note: open() is a built in function but close() is a function of file handle object.

Standard Input / Output / Error Streams: Our standard devices
1) Standard input device (stdin) : keyboard
2) Standard output device (stdout) : monitor
3) Standard error device (stderr) : monitor
are implemented as files called standard streams.
These streams are defined in sys module and so we need to import sys module to use any of these file streams.
We can print the any contents or string along with its length on output stream or error stream.

>>> import sys
>>> sys.stdout.write("Standard output stream")
Standard output stream22
>>> sys.stderr.write("Standard error stream")
Standard error stream21
error: You can only copy the programs code and output from this website. You are not allowed to copy anything else.