file is a contiguous set of bytes used to store data. This data is organized in a specific format .
Files on most modern file systems are composed of three main parts:
Header: metadata about the contents of the file (file name, size, type, and so on)
Data: contents of the file as written by the creator or editor
End of file (EOF): special character that indicates the end of the file
Folder Path: the file folder location on the file system where subsequent folders are separated by a forward slash / (Unix) or backslash \ (Windows)
File Name: the actual name of the file Extension: the end of the file path pre-pended with a period (.) used to indicate the file type
The double-dot (..) can be chained together to traverse multiple directories above the current directory.
ASA standard states that line endings should use the sequence of the Carriage Return (CR or \r) and the Line Feed (LF or \n) characters (CR+LF or \r\n).
Pug\r\n
Jack Russell Terrier\r\n
English Springer Spaniel\r\n
German Shepherd\r\n
Staffordshire Bull Terrier\r\n
Cavalier King Charles Spaniel\r\n
Golden Retriever\r\n
West Highland White Terrier\r\n
Boxer\r\n
Border Terrier\r\n
file = open('dog_breeds.txt')
you should always make sure that an open file is properly closed.
The first way to close a file is to use the try-finally block:
reader = open('dog_breeds.txt')
try:
# Further file processing goes here
finally:
reader.close()
"r" - Read
"x" - Create
"a" - Append
"w" - Write
"x" - Create
"t" - Text - Default value. Text mode
"b" - Binary - Binary mode
eturn one line by using the readline() looping through the lines of the file, you can read the whole file, line by line
To delete a file, you must import the OS module, and run its os.remove()
with open('dog_breeds.txt', 'a') as a_writer:
a_writer.write('\nBeagle')
(Syntax errors
occur when the parser detects an incorrect statement. )
raise allows you to throw an exception at any time. while enables you to verify if a certain condition is met and throw an exception if it isn’t. We can use raise to throw an exception if a condition occurs
**The AssertionError Exception Instead of waiting for a program to crash midway, you can also start by making an assertion in Python **
Python executes code following the try statement as a “normal” part of the program. The code that follows the except statement is the program’s response to any exceptions
instruct a program to execute a certain block of code only in the absence of exceptions.
lean up after executing your code