Files
Files
Also, in a file, a new line character (‘\n’) marks the end of a line or start of a new line.
Types of File
Text File: Text file usually we use to store character data. For example, test.txt
Binary File: The binary files are used to store binary data such as images, video files, audio files, etc.
File Path
A file path defines the location of a file or folder in the computer system. There are two ways to specify a file
path.
2. access_mode:
Access mode specifies what you want to do with the file. The default mode is read mode ‘r’.
It returns the file object. This object is used to read or write the file according to the access
mode. Accesss mode represents the purpose of opening the file. For example, R is for
reading and W is for writing
# Opening the file with absolute path
fp = open(r 'E:\demos\files\sample.txt', 'r')
# read file
print(fp.read())
# Closing the file after reading
fp.close()
Steps For Opening File in Python
Find the path of a file
Note:
If a file already exists, it truncates the existing content and places the filehandle at the beginning of the file. A new file
is created if the mentioned file doesn’t exist.
If you want to add content at the end of the file, use the access mode a to open a file in append mode
Use access mode w if you want to create and write content into a file.