21
loading...
This website collects cookies to deliver better user experience
test_file = open('test.txt', 'r')
first_line = test_file.readline()
# do something...
test_file.close()
with open('test.txt', 'r') as test_file:
first_line = test_file.readline()
# do something
with open('test.txt', 'w') as test_file:
print(test_file.readable()) # False
print(test_file.readline()) # error
with open('test.txt', 'r') as test_file:
print(test_file.readable()) # True
Hello, I'm Aya Bouchiha
This is an Example
Aya Bouchiha
Test
with open('test.txt', 'r') as test_file:
print(test_file.read(28))
# Hello, I'm Aya Bouchiha
# This
print(test_file.read())
# is an Example
# Aya Bouchiha
# Test
print(test_file.read())
# ""
Hello, I'm Aya Bouchiha
This is an Example
Aya Bouchiha
Test
with open('test.txt', 'r') as test_file:
first_line = test_file.readline()
second_line = test_file.readline()
fname_from_third_line = test_file.readline(3)
print(first_line) # Hello, I'm Aya Bouchiha
print(second_line) # This is an Example
print(fname_from_third_line) # Aya
Aya Bouchiha
[email protected]
https://t.me/AyaBouchiha
with open('user.txt', 'r') as user_file:
# [
# "Aya Bouchiha\n",
# "[email protected]\n",
# "https://t.me/AyaBouchiha\n",
# ]
print(user_file.readlines())
user_file.seek(0)
print(user_file.readlines(3))
# ["Aya Bouchiha\n"]
seek(pos): helps you to specifiy the cursor's position, and gets the new one. more details
tell(): lets you get the file's current position.
Hello, I'm Aya Bouchiha
This is an Example
Aya Bouchiha
Test
with open('test.txt', 'r') as test_file:
print(test_file.tell()) # 0
first_line = test_file.readline() # Hello, I'm Aya Bouchiha
current_position = test_file.seek(7) # "hello, " is ignored, len("hello, ") is 7
print(current_position) # 7
print(test_file.tell()) # 7
print(test_file.read())
# I'm Aya Bouchiha
# This is an Example
# Aya Bouchiha
# Test
with open('test.txt', 'r') as test_file:
print(test_file.readable()) # True
print(test_file.writable()) # False
with open('test.txt', 'w+') as test_file:
print(test_file.readable()) # True
print(test_file.writable()) # True
with open('message.txt', 'w') as message_file:
message = 'Good morning!'
message_file.write(message)
Good morning!
emails = ["[email protected]\n", "[email protected]",]
with open('emails.txt', 'w') as emails_file:
emails_file.writelines(emails)
admins = ["Aya Bouchiha\n", "John Doe\n", "Simon Spouf"]
with open('admins.txt', 'a+') as admins_file:
print(admins_file.readable()) # True
print(admins_file.writable()) # Trye
admins_file.write("Hi, welcome to admins.txt\n")
admins_file.write('admins are:\n')
admins_file.writelines(admins)
print(admins_file.tell()) # 75
admins_file.seek(0)
print(admins_file.read())
# Hi, welcome to admins.txt
# admins are:
# Aya Bouchiha
# John Doe
# Simon Spouf
Hi, welcome to admins.txt
admins are:
Aya Bouchiha
John Doe
Simon Spouf
with open('emails.txt', 'a+') as emails_file:
email_address = '[email protected]'
emails_file.truncate(len(email_address))
emails_file.seek(0)
# [email protected]
print(emails_file.read())
with open('emails.txt', 'a+') as emails_file:
emails = [
"[email protected]\n",
"[email protected]\n",
"[email protected]"
]
emails_file.writelines(emails)
emails_file.seek(0)
# [email protected]
print(emails_file.readline())
emails_file.truncate(len("".join(emails[:2])))
emails_file.seek(0)
# [email protected]
# [email protected]
print(emails_file.read())
emails_file.seek(len(emails[0]))
# size = cursor's position which is len(emails[0])
emails_file.truncate()
# 26
print(emails_file.tell())
emails_file.seek(0)
# [email protected]
print(emails_file.read())