0% found this document useful (0 votes)
9 views

File Handling & Exit Function

Uploaded by

amangupta121
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

File Handling & Exit Function

Uploaded by

amangupta121
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Exit()

Function
List of Python Exit Commands

• There are several commands you can use to exit a program


in Python. Some of these commands include:
• The quit() function
• The exit() function
• The sys.exit() function
• The os._exit() function
The quit() Function
• The first Python command to exit program we’ll discuss is
quit(). Python’s in-built quit() function exits a Python program
by closing the Python file. Since the quit() function requires
us to load the site module, it is generally not used in
production code
Example: quit()
for value in range(0,10):
# If the value becomes 6 then the program prints quit
# message and terminates via quit()
if value == 6:
# Prints the quit message
print(quit)
quit()
# Values get printed till the program terminates
print(value)
The exit() Function
• Python’s in-built exit() function is defined in site.py and is an
alias for quit(). It works only if the site module is imported.
Hence, it should not be used in production code, only in the
interpreter. It’s like a more user-friendly synonym of quit().
• Like quit(), exit() also raises SystemExit exception. Here,
exit(0) implies a clean exit without any issues, while exit(1)
implies some issue, which was the only reason for exiting the
program.
Python command to exit program:
exit() Example
for value in range(0,10):
# If the value becomes 6 then the program prints exit
# message and terminates via exit()
if value == 6:
#prints the exit message
print(exit)
exit()
# Values get printed till the program terminates
print(value)
File Handling
File Handling in Python
• File handling in Python is a powerful and versatile tool that can be
used to perform a wide range of operations.
• Python too supports file handling and allows users to handle files i.e.,
to read and write files, along with many other file handling options, to
operate on files
• Each line of code includes a sequence of characters and they form a
text file.
• Python has several functions for creating, reading, updating, and
deleting files.
There are four different methods
(modes) for opening a file:

• "r" - Read - Default value. Opens a file for reading,


error if the file does not exist
• "a" - Append - Opens a file for appending, creates the
file if it does not exist
• "w" - Write - Opens a file for writing, creates the file if it
does not exist
• "x" - Create - Creates the specified file, returns an error
if the file exists
• r+: To read and write data into the file. The previous data in the file
will be overridden.
• w+: To write and read data. It will override existing data.
• a+: To append and read data from the file. It won’t override existing
data.
In addition you can specify if the file should
be handled as binary or text mode

• "t" - Text - Default value. Text mode


• "b" - Binary - Binary mode (e.g. images)
Syntax

Because "r" for read, and "t" for text are the default values, you do not
need to specify them.
Advantages of File Handling
• Versatility: File handling in Python allows you to perform a wide range of
operations, such as creating, reading, writing, appending, renaming, and
deleting files.
• Flexibility: File handling in Python is highly flexible, as it allows you to
work with different file types (e.g. text files, binary files, CSV files, etc.),
and to perform different operations on files (e.g. read, write, append, etc.).
• User–friendly: Python provides a user-friendly interface for file handling,
making it easy to create, read, and manipulate files.
• Cross-platform: Python file-handling functions work across different
platforms (e.g. Windows, Mac, Linux), allowing for seamless integration
and compatibility.
Disadvantages of File Handling
• Error-prone: File handling operations in Python can be prone to errors,
especially if the code is not carefully written or if there are issues with the file
system (e.g. file permissions, file locks, etc.).
• Security risks: File handling in Python can also pose security risks, especially if
the program accepts user input that can be used to access or modify sensitive
files on the system.
• Complexity: File handling in Python can be complex, especially when working
with more advanced file formats or operations. Careful attention must be paid to
the code to ensure that files are handled properly and securely.
• Performance: File handling operations in Python can be slower than other
programming languages, especially when dealing with large files or performing
complex operations.
Open a File on the Server
• Assume we have the following file, located in the same
folder as Python:

Demofile.txt

Hello! Welcome to demofile.txt


This file is for testing purposes.
Good Luck!
• To open the file, use the built-in open() function.
• The open() function returns a file object, which has
a read() method for reading the content of the file:
If the file is located in a different location, you
will have to specify the file path, like this:
Read Only Parts of the File
Python File Write
• Write to an Existing File
• To write to an existing file, you must add a parameter
to the open() function:
• "a" - Append - will append to the end of the file
• "w" - Write - will overwrite any existing content
Demo file
example
Create a New File
• To create a new file in Python, use the open() method,
with one of the following parameters:
• "x" - Create - will create a file, returns an error if the
file exist
• "a" - Append - will create a file if the specified file does
not exist
• "w" - Write - will create a file if the specified file does
not exist
example
Python Delete File
• To delete a file, you must import the OS module, and
run its os.remove() function:
Delete Folder
• To delete an entire folder, use the os.rmdir() method:
Thank you!

You might also like