FILE HANDLING



Python - File Handling


Python File Open



File handling is an important part of any web application.

Python has several functions for creating, reading, updating, and deleting files.


File Handling :


The key function for working with files in Python is the open() function.

The open() function takes two parameters; filename, and mode.

There are four different methods (modes) for opening a file:


MethodDescription
"r" - ReadDefault value. Opens a file for reading, error if the file does not exist
"a" - AppendOpens a file for appending, creates the file if it does not exist
"w" - WriteOpens a file for writing, creates the file if it does not exist
"x" - CreateCreates the specified file, returns an error if the file exists

In addition you can specify if the file should be handled as binary or text mode.


MethodDescription
"t" - TextDefault value. Text mode
"b" - BinaryBinary mode (e.g. images)

Syntax :

To open a file for reading it is enough to specify the name of the file:

f = open("demofile.txt")

The code above is the same as:

f = open("demofile.txt", "rt")


Because "r" for read, and "t" for text are the default values, you do not need to specify them.


Note: Make sure the file exists, or else you will get an error.



RegEx SETS

Python - RegEx SETS

posted on 2019-11-12 23:06:24 - Python Tutorials


RegEx_Functions

Python - RegEx_Functions

posted on 2019-11-09 06:07:29 - Python Tutorials


RegEx_Sets

Python - RegEx_Sets

posted on 2019-11-09 05:30:54 - Python Tutorials


Prompt Examples

ChatGPT Prompt Examples

posted on 2023-06-21 22:37:19 - ChatGPT Tutorials


Use Cases

Chat GPT Key Use Cases

posted on 2023-06-21 21:03:17 - ChatGPT Tutorials


Prompt Frameworks

Prompt Frameworks

posted on 2023-06-21 19:33:06 - ChatGPT Tutorials