Python Tutorial: File Objects - Reading and Writing to Files

preview_player
Показать описание
In this Python Tutorial, we will be learning how to read and write to files. You will likely come into contact with file objects at some point while using Python, so knowing how to read and write from them is extremely important. We will learn how to read and write from simple text files, open multiple files at once, and also how to copy image binary files. Let's get started.

The code from this video can be found at:

Read more about opening in binary mode here:

✅ Support My Channel Through Patreon:

✅ Become a Channel Member:

✅ One-Time Contribution Through PayPal:

✅ Cryptocurrency Donations:
Bitcoin Wallet - 3MPH8oY2EAgbLVy7RBMinwcBntggi7qeG3
Ethereum Wallet - 0x151649418616068fB46C3598083817101d3bCD33
Litecoin Wallet - MPvEBY5fxGkmPQgocfJbxP6EmTo5UUXMot

✅ Corey's Public Amazon Wishlist

✅ Equipment I Use and Books I Recommend:

▶️ You Can Find Me On:

#Python
Рекомендации по теме
Комментарии
Автор

okay i'm at a point where i watch corey's videos just after waking up...

for fun.

alashawn
Автор

Maximum efficiency and Ultimate mastery of teaching has been shown in this video! You present your tutorials like a rabbi who read the scripture his whole life! Magnificent!

RameenFallschirmjager
Автор

No gimmicky loud background music or talking like he's some badass hacker...just clearly explained lessons that are easy to understand. Awesome work!

revinar
Автор

Thank You Corey! You are a talented and gifted Tutor. I have watched over 50 python tutorials and yours are the best example of what I have found on Youtube.

PaulGrahamJR
Автор

My Notes for this video. You can comment out all of it and uncomment and run one function at a time to revise what Corey has taught us:




# While opening a file we can specify whether we are opening the file for 'reading', 'writing', 'reading & writing' or 'appending'
# If we don't specify anything, it defaults to 'reading'
# f = open('text.txt', 'w') # Opens a file for writing
# f = open('text.txt', 'r+') # Opens a file for reading & writing
# f = open('text.txt', 'a') # Opens a file for appending
# Opens a file for reading

f = open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r')


print(f.name) # Returns the name of the open file
print(f.closed) # Returns whether the file associated with the variable is closed
print(f.mode) # Returns the mode in which it was opened i.e. 'r', 'w', 'r+', 'a'


# All files opened using an open() command need to be closed explicitly after their use is complete. If this is not done, we can end up with leaks that cause us to run over the maximum allowed file descriptors on the system and our app can throw an error.
f.close()

# We can avoid this problem with a context manager as below

# The below 'with open()' command will close the file as soon as the code has finished running or an error is thrown
with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:

# Code goes here
pass

# This produces the error "ValueError: I/O operation on closed file.""
# print(f.read())


with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
# This just reads all lines in the file
text_file_contents = text_file.read()
print(text_file_contents)



with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
# This returns a list that contains all lines in the file
text_file_contents = text_file.readlines()
print(text_file_contents)


with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
# This returns a line of text (not a list) at a time
# The first time we print .readline() it returns the first line
text_file_contents = text_file.readline()
print(text_file_contents)
# The second time we print .readline() it returns the second line
text_file_contents = text_file.readline()
print(text_file_contents)



with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
# This returns a line of text (not a list) at a time
# The first time we print .readline() it returns the first line
# Putting an 'end = '' in the return or print statement removes the newline between each result
text_file_contents = text_file.readline()
print(text_file_contents, end='')
# The second time we print .readline() it returns the second line
# Putting an 'end = '' in the return or print statement removes the newline between each result
text_file_contents = text_file.readline()
print(text_file_contents, end='')


# The above methods take a lot of storage as lines get stored in memory
# Iterating over lines in a file avoid this
with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
for line in text_file:
print(line, end='')


with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
# Passing a number to the .read() command preads just that number of characters
# The first time this is run, a 10 characters will be read and printed
text_file_contents = text_file.read(10)
print(text_file_contents, end='') # It returns '#1) This is'
# The second time this is run, the next 10 characters will be read and printed
text_file_contents = text_file.read(10)
print(text_file_contents, end='') # '1) This is a test fi'
# The same line is extended (without introducing a new line or a new returned value)
# When we reach the end of the file, read just reads what is left and returns an empty string for the rest of it
text_file_contents = text_file.read(1000)
print(text_file_contents, end='')

# The below code will print out the entire code

with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
size_to_read = 10
text_file_contents = text_file.read(size_to_read)
while len(text_file_contents) > 0:
print(text_file_contents, end='')
text_file_contents = text_file.read(size_to_read)


#
with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
size_to_read = 10
text_file_contents = text_file.read(size_to_read)
while len(text_file_contents) > 0:
# The '#' symbol in the output marks the chunks that were printed in each iteration
print(text_file_contents, end='#')
text_file_contents = text_file.read(size_to_read)


with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
size_to_read = 10
text_file_contents = text_file.read(size_to_read)
# The 'filename.tell()' returns the position of the file till where we've read until now
# This returns 10, since we've read 10 characters
print(text_file.tell())
text_file_contents = text_file.read(size_to_read)
# This returns 20, since we've read 10 more characters (10+10=20)
print(text_file.tell())


with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
size_to_read = 10
# Reads the first 10 characters. Read position set to 10
text_file_contents = text_file.read(size_to_read)
print(text_file.tell()) # Prints 10

# Reads the next 10 characters. Read position set to 10+10=20
text_file_contents = text_file.read(size_to_read)
print(text_file.tell()) # Prints 20

# filename.seek() sets the read position to whatever character we set it to. Here set to 0.
text_file.seek(0) # Sets the read position to 0
print(text_file.tell()) # Prints 0

# Reads the first 10 characters. Read position set to 10
text_file_contents = text_file.read(size_to_read)
print(text_file.tell()) # Prints 10


# If we try to write to a file that is opened for reading. An error is produced.
# Error = 'io.UnsupportedOperation: not writable'
# with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
# text_file.write('Test')

# If a file with this name doesn't already exist. It will be created.
# If a file does exist, it will be overwritten
with open('C:/#Personal/Coding Projects/Proj1/Py edn/text2.txt', 'w') as text_file:
text_file.write('Test')



# If you don't want to overwrite a file, use an 'append' setting by passing a lowercase a
with open('C:/#Personal/Coding Projects/Proj1/Py edn/text2.txt', 'a') as text_file:
text_file.write('Test')


with open('C:/#Personal/Coding Projects/Proj1/Py edn/text2.txt', 'w') as text_file:
text_file.write('Test') # File contains: 'Test'
# filename.seek(position) sets the write position to the number we pass in
text_file.seek(0)
# If we now write something. It will be written from the newly set position.
# It will overwrite anything in its path for as many characters it need to overwrite
text_file.write('LN') # File contains: 'LNst'.
# The first 2 characters from position 0 were overwritten because it was required



# Copying from one file to another, line by line
# This can't be done for image files. It shows an error. Invalid start byte. Copying an image file would require opening it in binary mode. We would be reading/writing bytes instead of text.
with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
with open('C:/#Personal/Coding Projects/Proj1/Py edn/textcopy.txt', 'w') as copy_file:
for line in text_file:
copy_file.write(line)


# To read binary we change open(filename, 'r') to open(filename, 'rb')
# To read binary we change open(filename, 'w') to open(filename, 'wb')
# The below code with these changes, can copy an image file
with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'rb') as text_file:
with open('C:/#Personal/Coding Projects/Proj1/Py edn/textcopy.txt', 'wb') as copy_file:
for line in text_file:
copy_file.write(line)



# Copying a file using chunks instead of line by line is better. This can be done by using the .read function we've studied above
with open('C:/#Personal/Coding Projects/Proj1/Py edn/text.txt', 'r') as text_file:
with open('C:/#Personal/Coding Projects/Proj1/Py edn/textcopy.txt', 'w') as copy_file:
chunk_size = 10
chunk = text_file.read(chunk_size)
while len(chunk) > 0:
copy_file.write(chunk)
chunk = text_file.read(chunk_size)

HarmanHundal
Автор

Dude, I'm in love with your python series. Keep up the amazing work.

stratan
Автор

although I am discovering this 8 years after I still find it very awesome. Keep up the good work man

henry-zhrv
Автор

The way this videos are systematically put together makes everything so easy to understand.
Watching your videos is so helpful and motivating .
Thank you Corey!

zsmclxg
Автор

Really helpful explanation of read & writing to files. This was quick and succinct. Helped me understand a few things that others don't cover. I love how you covering just the right stuff.

PoeLemic
Автор

This video is so well explained. I’m a beginner and am able to follow everything perfectly!

worstbuffs
Автор

Yours are some of the best python tutorials I've found on YouTube. No bs. Very well explained. Cheers mate

tehemtoncode
Автор

This man deserves a knighthood.
In the name of the Warrior I charge you Ser Corey Schafer to be brave.
In the name of the Father I charge you to be just.
In the name of the teacher I charge you to educate the masses.
In the

Arise Ser Corey Schafer:)

pilaramonkgogimoshebashebi
Автор

Thank you so much!!!! Your video is extraordinary. You explain these concepts clearly and thoroughly, in a very engaging way with practical examples. You made me understand this topic very quickly. Thanks again!

estefaniac
Автор

You all tutorials are stright forward and cover all aspect of a specific topic. Thank you for this channel.

farazahmed
Автор

You have the clearest, best explained videos on Python. Great job.

officesuperhero
Автор

This really helped me understand how to read and write to files. Thank you for your time and expertise.

Xaminn
Автор

These must be the classes from the Corey Schafer University.
Absolutely fantastic videos !
Probably the best and most detailed tutorials out there.

businesscontact
Автор

Brilliant, A good teacher can make you remember concepts with ease. I am delighted to gain from your teachings. Thanks a ton.

DataEngineeringGeek
Автор

these are better than my 2 hours lectures... My professor sucks so much at teaching python. These are amazing and a life saver!

chandrasekharlimit
Автор

Never saw a mentor like you before.
You are the one who teaches fellows like me for free.
Thank you so much sir.

saurabhyelmame