- Python Tutorial: File Objects - Reading and Writing to Files

Python Tutorial: File Objects - Reading and Writing to Files

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 mu...
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:
https://github.com/CoreyMSchafer/code_snippets/tree/master/Python-Files

Read more about opening in binary mode here:
https://docs.python.org/3/library/functions.html#open


✅ Support My Channel Through Patreon:
https://www.patreon.com/coreyms

✅ Become a Channel Member:
https://www.youtube.com/channel/UCCezIgC97PvUuR4_gbFUs5g/join

✅ One-Time Contribution Through PayPal:
https://goo.gl/649HFY

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

✅ Corey's Public Amazon Wishlist
http://a.co/inIyro1

✅ Equipment I Use and Books I Recommend:
https://www.amazon.com/shop/coreyschafer

▶️ You Can Find Me On:
My Website - http://coreyms.com/
My Second Channel - https://www.youtube.com/c/coreymschafer
Facebook - https://www.facebook.com/CoreyMSchafer
Twitter -
Instagram - https://www.instagram.com/coreymschafer/

#Python

#Python #Python Files #File io #Python File io #Python Read File #Python Write File #Read File #Write File #File #Files #Python File i/o #File i/o #Python File Read #Python File Write #Copy File #Python Tutorial #Python Tutorials #file i/o tutorial #Learn Python #Python open #Context Manager #Context Managers #Python for Beginners #Programming Tutorials #Software Engineering #open file #Python open file
Nothing bad at - Python Tutorial: File Objects - Reading and Writing to Files

Nothing bad at

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:04:05 - 00:24:33
Great Video! I would just like to know how you commented out a big chunk of code at  :) - Python Tutorial: File Objects - Reading and Writing to Files

Great Video! I would just like to know how you commented out a big chunk of code at :)

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:08:25 - 00:24:33
Great video,.Corey. quick question: how did you mark out the code at ? I am trying to put " in front of my code over 100 lines, any suggestions how that could be done in atom or pycharm? - Python Tutorial: File Objects - Reading and Writing to Files

Great video,.Corey. quick question: how did you mark out the code at ? I am trying to put " in front of my code over 100 lines, any suggestions how that could be done in atom or pycharm?

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:08:28 - 00:24:33
Do you have a video on all the shortcuts you use when writing the code? For example, I just noticed at  when you said, “let me comment out these lines”, then you highlighted the lines, and magically had them all commented out in one swift motion.I am super new, but learning those would be SUPER beneficial. Thanks!! - Python Tutorial: File Objects - Reading and Writing to Files

Do you have a video on all the shortcuts you use when writing the code? For example, I just noticed at when you said, “let me comment out these lines”, then you highlighted the lines, and magically had them all commented out in one swift motion.I am super new, but learning those would be SUPER beneficial. Thanks!!

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:08:30 - 00:24:33
a little question, hope someone can answer, how to make scrips to become commants? and it be done vise-versa? like in , he had done - Python Tutorial: File Objects - Reading and Writing to Files

a little question, hope someone can answer, how to make scrips to become commants? and it be done vise-versa? like in , he had done

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:08:40 - 00:24:33
At  for reading the file, a loop is done: - Python Tutorial: File Objects - Reading and Writing to Files

At for reading the file, a loop is done:

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:08:55 - 00:24:33
When he says that the code written as is would initiate an infinite loop at  , would the output if he did run it just be the first 100 characters over and over again? (lines 1 through 5 of his test.txt) Or would it print the entire test.txt over and over? - Python Tutorial: File Objects - Reading and Writing to Files

When he says that the code written as is would initiate an infinite loop at , would the output if he did run it just be the first 100 characters over and over again? (lines 1 through 5 of his test.txt) Or would it print the entire test.txt over and over?

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:11:25 - 00:24:33
Thanks for this tutorial, very practical, explaining how read() can cause a memory bottleneck if the file is huge. One question though ... unable to understand at  how while loops executes its last iteration and exits. From the output its clear that only 4 characters remain to be read at the start of last iteration which is still greater than 0. Can someone elaborate? - Python Tutorial: File Objects - Reading and Writing to Files

Thanks for this tutorial, very practical, explaining how read() can cause a memory bottleneck if the file is huge. One question though ... unable to understand at how while loops executes its last iteration and exits. From the output its clear that only 4 characters remain to be read at the start of last iteration which is still greater than 0. Can someone elaborate?

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:12:25 - 00:24:33
for writing file . Np . Thanks for the vid man really helped me out in the write mode part <3 - Python Tutorial: File Objects - Reading and Writing to Files

for writing file . Np . Thanks for the vid man really helped me out in the write mode part <3

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:14:44 - 00:24:33
Writing to Files - Python Tutorial: File Objects - Reading and Writing to Files

Writing to Files

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:14:45 - 00:24:33
Careful! File contents(if exists) are erased the moment it's opened in write mode. - Python Tutorial: File Objects - Reading and Writing to Files

Careful! File contents(if exists) are erased the moment it's opened in write mode.

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:16:10 - 00:24:33
Why did 'R'' did not overwrite 'Test' since it is 'write' command not 'append'? - Python Tutorial: File Objects - Reading and Writing to Files

Why did 'R'' did not overwrite 'Test' since it is 'write' command not 'append'?

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:18:04 - 00:24:33
at   the for loop has statement:  wf.write (line) > this must be in 'a' mode to keep all the lines copied to the newly generated file .. or  I am missing something? - Python Tutorial: File Objects - Reading and Writing to Files

at the for loop has statement: wf.write (line) > this must be in 'a' mode to keep all the lines copied to the newly generated file .. or I am missing something?

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:19:57 - 00:24:33
at , if you remove the text from the write file and re-run the program it does not write in the file again it just leaves it blank. In order to use the function to write into the file, I have to create a new write file. Why is this? - Python Tutorial: File Objects - Reading and Writing to Files

at , if you remove the text from the write file and re-run the program it does not write in the file again it just leaves it blank. In order to use the function to write into the file, I have to create a new write file. Why is this?

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:20:18 - 00:24:33
And, I have a little question @ of the video. - Python Tutorial: File Objects - Reading and Writing to Files

And, I have a little question @ of the video.

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:20:35 - 00:24:33
one of the reasons why liner algebra is useful in cs - Python Tutorial: File Objects - Reading and Writing to Files

one of the reasons why liner algebra is useful in cs

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:21:30 - 00:24:33
first things first very thnks for your hard works your lessons very awesome , in  i think  you don't need loop you can just do like this:with open('bronx.jpg', 'rb') as rf:with open('copy_bronx.jpg', 'wb') as wf:wf.write(rf.read()) - Python Tutorial: File Objects - Reading and Writing to Files

first things first very thnks for your hard works your lessons very awesome , in i think you don't need loop you can just do like this:with open('bronx.jpg', 'rb') as rf:with open('copy_bronx.jpg', 'wb') as wf:wf.write(rf.read())

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:22:11 - 00:24:33
i dont understand how the while loop works here(), rf_chunk = rf.read(chunk_size) how this keep in not being in infinite loop. - Python Tutorial: File Objects - Reading and Writing to Files

i dont understand how the while loop works here(), rf_chunk = rf.read(chunk_size) how this keep in not being in infinite loop.

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:23:36 - 00:24:33
on , how does the last line of the while loop prevents infinite loop? - Python Tutorial: File Objects - Reading and Writing to Files

on , how does the last line of the while loop prevents infinite loop?

Python Tutorial: File Objects - Reading and Writing to Files
2016年04月30日 
00:23:38 - 00:24:33

Corey Schafer

※本サイトに掲載されているチャンネル情報や動画情報はYouTube公式のAPIを使って取得・表示しています。

Timetable

動画タイムテーブル

動画数:236件

how to override existing environment variables. (This is new to me) - Python Tutorial: Securely Manage Passwords and API Keys with DotEnv

how to override existing environment variables. (This is new to me)

Python Tutorial: Securely Manage Passwords and API Keys with DotEnv
2024年04月18日  @JorgeEscobarMX 様 
00:10:42 - 00:16:58
the future spoken is my current present. Unless I'm completely desperate, I'm not hitting reddit or google/stack overflow for most roadblocks. - How to Use ChatGPT as a Powerful Tool for Programming

the future spoken is my current present. Unless I'm completely desperate, I'm not hitting reddit or google/stack overflow for most roadblocks.

How to Use ChatGPT as a Powerful Tool for Programming
2023年05月22日  o k t o b e r 様 
00:12:22 - 00:31:08
Good video as always😉  I remember asking that too lol - How to Use ChatGPT as a Powerful Tool for Programming

Good video as always😉 I remember asking that too lol

How to Use ChatGPT as a Powerful Tool for Programming
2023年05月22日  kami 様 
00:28:19 - 00:31:08
@ - pure gold, pure gold! <3 - How to Use ChatGPT as a Powerful Tool for Programming

@ - pure gold, pure gold! <3

How to Use ChatGPT as a Powerful Tool for Programming
2023年05月22日  ulf gjerdingen 様 
00:28:26 - 00:31:08
You almost had me there haha! ChatGPT can be wrong but it certainly wasn't wrong about those top notch resources! I massively appreciate all the uploads and I hope they keep coming supercharged with Chat GPT. Thanks to you, sentdex, techwithtim and Dennis Ivy I now know how to properly code on python, use git for version control and build a functional website. - How to Use ChatGPT as a Powerful Tool for Programming

You almost had me there haha! ChatGPT can be wrong but it certainly wasn't wrong about those top notch resources! I massively appreciate all the uploads and I hope they keep coming supercharged with Chat GPT. Thanks to you, sentdex, techwithtim and Dennis Ivy I now know how to properly code on python, use git for version control and build a functional website.

How to Use ChatGPT as a Powerful Tool for Programming
2023年05月22日  Uj Vag 様 
00:28:42 - 00:31:08
and it's  am here (south east asia), and idk why I'm watching a python tutorial rather than sleeping 😂 - Python Tutorial: Simulate the Powerball Lottery Using Python

and it's am here (south east asia), and idk why I'm watching a python tutorial rather than sleeping 😂

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  X 様 
00:02:49 - 00:38:56
… nice… 😂 - Python Tutorial: Simulate the Powerball Lottery Using Python

… nice… 😂

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  Bartosz Turkowyd 様 
00:04:50 - 00:38:56
I've seen every Python video you've made....   first innuendo. 😜 - Python Tutorial: Simulate the Powerball Lottery Using Python

I've seen every Python video you've made.... first innuendo. 😜

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  Martin Beaudry 様 
00:04:52 - 00:38:56
Man it's  am in India I am lucky to be the first one to hear your voice god bless you brother and an awesome teacher. - Python Tutorial: Simulate the Powerball Lottery Using Python

Man it's am in India I am lucky to be the first one to hear your voice god bless you brother and an awesome teacher.

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  Big Samosa 様 
00:12:15 - 00:38:56
# you can also use the built-in pprint module for pretty printing dictionaries ;) - Python Tutorial: Simulate the Powerball Lottery Using Python

# you can also use the built-in pprint module for pretty printing dictionaries ;)

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  Schoenling Wunderbar 様 
00:25:00 - 00:38:56
it would be very interesting to see if there could be a step function where this algorithm can simulate the increases in jackpot winnings. It’s going to be one in 292+ million of a chance that you hit it at all much less multiple times, but the code should be interesting to see. - Python Tutorial: Simulate the Powerball Lottery Using Python

it would be very interesting to see if there could be a step function where this algorithm can simulate the increases in jackpot winnings. It’s going to be one in 292+ million of a chance that you hit it at all much less multiple times, but the code should be interesting to see.

Python Tutorial: Simulate the Powerball Lottery Using Python
2023年01月10日  20 80 様 
00:27:43 - 00:38:56
A OAuth Django video would be awesome!(Applicable to all sorts of OAuth situations, not just the YouTube API.) - Python YouTube API Tutorial: Using OAuth to Access User Accounts

A OAuth Django video would be awesome!(Applicable to all sorts of OAuth situations, not just the YouTube API.)

Python YouTube API Tutorial: Using OAuth to Access User Accounts
2020年09月10日 
00:07:58 - 00:43:21
At  you move the file and see some blurred miniature version of your open folders in the dock. How did you enable that? - Python YouTube API Tutorial: Using OAuth to Access User Accounts

At you move the file and see some blurred miniature version of your open folders in the dock. How did you enable that?

Python YouTube API Tutorial: Using OAuth to Access User Accounts
2020年09月10日 
00:09:58 - 00:43:21
In case you are getting Error 403 ("access_denied The developer hasn’t given you access to this app") go to the OAuth consent screen and under Test Users add your email by clicking on Add Users.  You should be able to grant access after you've added User. - Python YouTube API Tutorial: Using OAuth to Access User Accounts

In case you are getting Error 403 ("access_denied The developer hasn’t given you access to this app") go to the OAuth consent screen and under Test Users add your email by clicking on Add Users. You should be able to grant access after you've added User.

Python YouTube API Tutorial: Using OAuth to Access User Accounts
2020年09月10日 
00:19:36 - 00:43:21
@corey  So why did you add the join function here, when appending the video id to the list its already a list and its working for me when I pass the vid_ids directly - Python YouTube API Tutorial: Calculating the Duration of a Playlist

@corey So why did you add the join function here, when appending the video id to the list its already a list and its working for me when I pass the vid_ids directly

Python YouTube API Tutorial: Calculating the Duration of a Playlist
2020年06月11日 
00:11:24 - 00:37:38
TypeError: expected string or bytes-like object, I am taking this error in , any idea? - Python YouTube API Tutorial: Calculating the Duration of a Playlist

TypeError: expected string or bytes-like object, I am taking this error in , any idea?

Python YouTube API Tutorial: Calculating the Duration of a Playlist
2020年06月11日 
00:18:50 - 00:37:38
(I love that "Whoops that went too well"-moment at  :D) - Python Tutorial: Real World Example - Using Patreon API and Pillow to Automate Image Creation

(I love that "Whoops that went too well"-moment at :D)

Python Tutorial: Real World Example - Using Patreon API and Pillow to Automate Image Creation
2020年05月11日 
00:35:19 - 00:52:33