0% found this document useful (0 votes)
27 views2 pages

Textfile

The document discusses different file access modes and methods for reading and writing text files in Python. It covers opening files in read, write, append modes and using methods like read(), readline(), readlines() to read file contents and write() to write to files. Examples are given to demonstrate these concepts.

Uploaded by

lakemas535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

Textfile

The document discusses different file access modes and methods for reading and writing text files in Python. It covers opening files in read, write, append modes and using methods like read(), readline(), readlines() to read file contents and write() to write to files. Examples are given to demonstrate these concepts.

Uploaded by

lakemas535
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Reading and Writing to text files:

Text files: Each line of text is terminated with a special character called EOL (End of Line), which is
the new line character (‘\n’) in python by default.
Six File Access Modes:
Read Only (‘r’) : Open text file for reading. If no file exists: I/O error. default mode.
Read and Write (‘r+’): Open the file for reading and writing. If no file exists: I/O error.
Write Only (‘w’) : Open the file for writing. If file exists, the data is truncated and over-written.
The handle is positioned at the beginning of the file. Creates the file if the file does not exist.
Write and Read (‘w+’) : Open the file for reading and writing. If file exists, the data is truncated
and over-written. The handle is positioned at the beginning of the file.
Append Only (‘a’): Open the file for writing. Creates the file if the file does not exist. The
handle is positioned at the end of the file. The data being written will be inserted at the end, after
the existing data.
Append and Read (‘a+’) : Open the file for reading and writing. Creates the file if the file does
not exist. The handle is positioned at the end of the file. The data being written will be inserted at
the end, after the existing data.
Points:
No module is required to be imported for this open() function.
(File_object = open(r"File_Name","Access_Mode")

# Program to show various ways to read and


# write data in a file.
file1 = open("myfile.txt","w")
L = ["This is Delhi \n","This is Paris \n","This is London \n"]

# \n is placed to indicate EOL (End of Line)


file1.write("Hello \n")#inserts data in a single line.
file1.writelines(L)#insert multiple strings at a single time(L is List).
file1.close() #to change file access modes

file1 = open("myfile.txt","r+")

print("Output of Read function is ")


print(file1.read())#reads the entire file(reads n bytes, if no n specified).
print()

# seek(n) takes the file handle to the nth


# byte from the beginning.
file1.seek(0)

print( "Output of Readline function is ")


print(file1.readline())#does not reads more than one line,
#even if n exceeds the length of the line.
print()

file1.seek(0)

# To show difference between read and readline


print("Output of Read(9) function is ")
print(file1.read(9))
print()

file1.seek(0)

print("Output of Readline(9) function is ")


print(file1.readline(9))
file1.seek(0)
# readlines function
print("Output of Readlines function is ")
print(file1.readlines())#Reads all the lines and return them as list.
print()
file1.close()
'''
OUTPUT
Output of Read function is
Hello
This is Delhi
This is Paris
This is London

Output of Readline function is


Hello

Output of Read(9) function is


Hello
Th

Output of Readline(9) function is


Hello

Output of Readlines function is


['Hello \n', 'This is Delhi \n', 'This is Paris \n', 'This is London \n']
'''

You might also like