Mike Driscoll Profile picture
Feb 16 8 tweets 4 min read
If you want to use #Python to read and write text or binary files, then you'll need to familiarize yourself with the built-in `open()` function!

Join me for this thread to learn more!

#python_builtins_by_driscollis

🧵🐍👇
#Python's `open()` function actually has quite a few different parameters that you can use:
The most commonly used parameters you will use with #Python's `open()` function are:

🐍 file - The path to the file
🐍 mode - Read, write, binary, etc
🐍 encoding - What encoding the file is in
Here is a listing of the various modes you can use when you open a file.

Note that some of the modes can be combined. For example "rb" means read-binary while "w+" means open and truncate.
The most common way to open a file in #Python is to use a `with` statement.

When you open a file this way, it will close the file handler object for you automatically.
If you don't use #Python's `with` statement when opening the file, you would write the code like this.

Note that it is best practice to close the file when you are done with it.

You should also wrap file I/O in a try/except in most cases to catch errors! 🐍⚠️
For more information about using #Python's `open()` function, you can check out the documentation page here:

docs.python.org/3/library/func…
Thanks for checking out my overview of #Python's `open()` function.

Follow me to learn more about the Python programming language! 🐍🔥

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Mike Driscoll

Mike Driscoll Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @driscollis

Feb 18
Today we are going to talk a little about creating command-line applications using #Python and its built-in `argparse` package!

🧵🐍👇
In this example of using `argparse`, you don't even create any custom arguments! You just create an ArgumentParser argument and call `print_help()`!

You can see in this example that `argparse` already provides useful information to your users without adding any arguments! Image
Normally when you create an `ArgumentParser` object, you would do so in a function and return the object.

Let's rewrite the previous example to follow that coding pattern: Image
Read 11 tweets
Feb 17
Even if you are a beginner, you have probably used #Python's built-in `print()` function.

But let's take a few minutes to talk about the humble `print()` function and see what you might have missed!

#python_builtins_by_driscollis

🧵🐍👇
The `print()` function takes the following arguments:

🐍 *objects - Stuff to print
🐍 sep=' ' - A string to separate object
🐍 end='\n' - A string to end on
🐍 file=sys.stdout - Where to write the data
🐍 flush=False - Whether or not to force flush the stream
Let's look at some examples of using Python's `print()` function: Image
Read 5 tweets
Feb 17
#Python has an awesome library called `collections`! One of my favorite tools from that library is the `Counter` class.

Here I use `Counter` to find the most common occurrences of a value in a list: Image
Let's use `collections.Counter()` to count letters in a string!

Note that the object that is returned by the Counter class can be accessed like a dictionary! Image
Python's `collections.Counter()` class has special methods that you can use to `subtract()` items from the counter object.

Here's an example: Image
Read 4 tweets
Feb 16
We have already learned about the `bin()` and `hex()` functions. Today you will learn about #Python's handy `oct()` function!

`oct()` is used to convert an integer number to an octal string prefixed with “0o”

#python_builtins_by_driscollis

🧵🐍👇
Here are a couple of examples of using the `oct()` function with positive and negative numbers:
The #Python documentation has a nice example that shows how to format octal numbers using string formatting:
Read 4 tweets
Feb 15
Blockchain is a super popular topic here on Twitter. While I am not a blockchain developer, I thought it would be fun to put together a short thread on blockchain and #Python

Check it out if you're interested in this topic!

🧵🐍👇
ActiveState is a well-respected #Python company with lots of great content and they have an intro article on blockchain with Python:

activestate.com/blog/how-to-bu… @ActiveState
@ActiveState The GeekFlare website also has a popular article on creating a blockchain with #Python:

geekflare.com/create-a-block…
Read 7 tweets
Feb 15
Python includes a built-in named `object`. This built-in isn't a function. It's actually a class.

The `object` class is the base class of all Python classes. It has methods that are common to all instances of Python classes.

#python_builtins_by_driscollis

🧵🐍👇
The following two examples show equivalent empty classes in Python.

They both subclass `object`: Image
You can verify that a class is a subclass of a specific class using Python's built-in `issubclass()`. Image
Read 4 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

:(