|
| https://realpython.com/ |
| Start Here | https://realpython.com/start-here/ |
|
Learn Python
| https://realpython.com/videos/python-maze-binary-file/ |
| Your Learning Plan →Your personalized Python learning plan | https://realpython.com/account/learning-plan/ |
| Python Tutorials →In-depth articles and video courses | https://realpython.com/search?kind=article&kind=course&order=newest&_from=nav_library |
| Learning Paths →Guided study plans for accelerated learning | https://realpython.com/learning-paths/ |
| Quizzes & Exercises →Check your learning progress | https://realpython.com/search?kind=exercise&kind=quiz&_from=nav_library |
| Browse Topics →Focus on a specific area or skill level | https://realpython.com/tutorials/all/ |
| Reference →Concise definitions for common Python terms | https://realpython.com/ref/ |
| Mentor AI →NewYour personal Python mentor | https://realpython.com/mentor/ |
| Office Hours →Live Q&A calls with Python experts | https://realpython.com/office-hours/ |
| Community Chat →Learn with other Pythonistas | https://realpython.com/community/ |
| Live Courses →Live, instructor-led Python courses | https://realpython.com/live/ |
| Books →Round out your knowledge and learn offline | https://realpython.com/products/books/ |
| Podcast →Hear what’s new in the world of Python | https://realpython.com/podcasts/rpp/ |
| Unlock All Content and Become a Python Expert →Get unlimited access to all Real Python has to offer | https://realpython.com/account/join/?from=navbar_learn |
| Unlock All Content →Become a Python Expert | https://realpython.com/account/join/?from=navbar_learn |
|
More
| https://realpython.com/videos/python-maze-binary-file/ |
| Learner Stories | https://realpython.com/learner-stories/ |
| Python Newsletter | https://realpython.com/newsletter/ |
| Python Job Board | https://www.pythonjobshq.com |
| Meet the Team | https://realpython.com/team/ |
| Become a Contributor | https://realpython.com/jobs/ |
| Search | https://realpython.com/search |
| https://realpython.com/search |
| Join | https://realpython.com/account/join/ |
| Sign‑In | https://realpython.com/account/login/?intent=navbar&next=%2Fvideos%2Fpython-maze-binary-file%2F |
|
| https://realpython.com/courses/python-maze-solver/#team |
| Mazes in Python: Build, Visualize, Store, and Solve | https://realpython.com/courses/python-maze-solver/ |
| Darren Jones | https://realpython.com/courses/python-maze-solver/#team |
| Recommended Tutorial | https://realpython.com/python-maze-solver/ |
| Course Slides (.pdf) | https://realpython.com/courses/python-maze-solver/downloads/mazes-in-python-slides/ |
| Sample Code (.zip) | https://realpython.com/courses/python-maze-solver/downloads/mazes-in-python-code/ |
| Ask a Question | https://realpython.com/videos/python-maze-binary-file/#discussion |
| https://realpython.com/feedback/survey/course/python-maze-solver/liked/?from=lesson-title |
| https://realpython.com/feedback/survey/course/python-maze-solver/disliked/?from=lesson-title |
| Contents | https://realpython.com/videos/python-maze-binary-file/#description |
| Transcript | https://realpython.com/videos/python-maze-binary-file/#transcript |
| Discussion | https://realpython.com/videos/python-maze-binary-file/#discussion |
| 00:00 | https://realpython.com/videos/python-maze-binary-file/#t=0.53 |
| Load the Maze From a Binary File. | https://realpython.com/videos/python-maze-binary-file/#t=0.53 |
| After completing part one of this course, you can build and draw the maze. | https://realpython.com/videos/python-maze-binary-file/#t=3.9699999999999998 |
| However, | https://realpython.com/videos/python-maze-binary-file/#t=8.23 |
| creating it by hand in Python takes a lot of error-prone and tedious effort. | https://realpython.com/videos/python-maze-binary-file/#t=9.2 |
| 00:13 | https://realpython.com/videos/python-maze-binary-file/#t=13.8 |
| By the end of this step, | https://realpython.com/videos/python-maze-binary-file/#t=13.8 |
| you’ll have a custom file format for the persistent storage of mazes, | https://realpython.com/videos/python-maze-binary-file/#t=14.74 |
| which you’ll be able to save and load at the push of a button. | https://realpython.com/videos/python-maze-binary-file/#t=18.47 |
| 00:22 | https://realpython.com/videos/python-maze-binary-file/#t=22.029 |
| Besides that, | https://realpython.com/videos/python-maze-binary-file/#t=22.029 |
| you’ll also be able to load the sample mazes supplied with the course materials. | https://realpython.com/videos/python-maze-binary-file/#t=22.96 |
| Squares in the maze are very much like pixels in an image. | https://realpython.com/videos/python-maze-binary-file/#t=29.61 |
| 00:33 | https://realpython.com/videos/python-maze-binary-file/#t=33.28 |
| One of the sample mazes included in the supporting materials has several hundred | https://realpython.com/videos/python-maze-binary-file/#t=33.28 |
| of them, | https://realpython.com/videos/python-maze-binary-file/#t=37.02 |
| but you could very easily be looking at thousands of squares in some larger | https://realpython.com/videos/python-maze-binary-file/#t=37.64 |
| mazes. | https://realpython.com/videos/python-maze-binary-file/#t=41.55 |
| 00:41 | https://realpython.com/videos/python-maze-binary-file/#t=41.94 |
| If you were to represent the squares as text, | https://realpython.com/videos/python-maze-binary-file/#t=41.94 |
| then they would quickly amount to a huge file size. | https://realpython.com/videos/python-maze-binary-file/#t=44.65 |
| Given that you already devised a compact representation for border patterns that | https://realpython.com/videos/python-maze-binary-file/#t=48.12 |
| leverages a bit field, it makes perfect sense to define a binary file format | https://realpython.com/videos/python-maze-binary-file/#t=52.19 |
| instead. | https://realpython.com/videos/python-maze-binary-file/#t=56.34 |
| 00:57 | https://realpython.com/videos/python-maze-binary-file/#t=57.316 |
| When defining a binary file format, | https://realpython.com/videos/python-maze-binary-file/#t=57.316 |
| the first thing you need to decide on is whether you’ll store values that can | https://realpython.com/videos/python-maze-binary-file/#t=61.72 |
| fit in a single byte or more. | https://realpython.com/videos/python-maze-binary-file/#t=65.32 |
| The reason is that different computer architectures have different endianness, | https://realpython.com/videos/python-maze-binary-file/#t=67.72 |
| which is a fancy way of saying that they read a sequence of bytes in the | https://realpython.com/videos/python-maze-binary-file/#t=71.99 |
| opposite order. | https://realpython.com/videos/python-maze-binary-file/#t=75.38 |
| 01:16 | https://realpython.com/videos/python-maze-binary-file/#t=76.5 |
| The | https://realpython.com/videos/python-maze-binary-file/#t=76.5 |
| two standards for byte order are called big-endian and little-endian. | https://realpython.com/videos/python-maze-binary-file/#t=78.53 |
| In big-endian, the most significant byte comes first, while in little-endian, | https://realpython.com/videos/python-maze-binary-file/#t=83.49 |
| the least significant byte comes first. | https://realpython.com/videos/python-maze-binary-file/#t=88.33 |
| 01:33 | https://realpython.com/videos/python-maze-binary-file/#t=93.01 |
| If you wrote a numeric value using one standard, but read it with another, | https://realpython.com/videos/python-maze-binary-file/#t=93.01 |
| then you’ll get a garbled result, | https://realpython.com/videos/python-maze-binary-file/#t=96.58 |
| just as if you applied the wrong character encoding. | https://realpython.com/videos/python-maze-binary-file/#t=98.26 |
| 01:44 | https://realpython.com/videos/python-maze-binary-file/#t=104.46 |
| You are going to need more than one byte to store values like the width or | https://realpython.com/videos/python-maze-binary-file/#t=104.46 |
| height of the maze, which can be far greater than the maximum byte value of 255. | https://realpython.com/videos/python-maze-binary-file/#t=108.0 |
| 01:53 | https://realpython.com/videos/python-maze-binary-file/#t=113.32 |
| Because many popular file formats stick with a little-endian standard, | https://realpython.com/videos/python-maze-binary-file/#t=113.32 |
| so will you. | https://realpython.com/videos/python-maze-binary-file/#t=117.07 |
| Binary files often begin with a fixed sequence of bytes to uniquely identify | https://realpython.com/videos/python-maze-binary-file/#t=120.43 |
| their particular format. | https://realpython.com/videos/python-maze-binary-file/#t=124.43 |
| 02:06 | https://realpython.com/videos/python-maze-binary-file/#t=126.114 |
| For example, | https://realpython.com/videos/python-maze-binary-file/#t=126.114 |
| Java’s class files always start with the four bytes seen on-screen, | https://realpython.com/videos/python-maze-binary-file/#t=127.45 |
| which is one of the geeky references to coffee in the language’s vast ecosystem. | https://realpython.com/videos/python-maze-binary-file/#t=130.96 |
| 02:16 | https://realpython.com/videos/python-maze-binary-file/#t=136.45 |
| Such an initial value is known as the magic number, | https://realpython.com/videos/python-maze-binary-file/#t=136.45 |
| although it can sometimes be textual. | https://realpython.com/videos/python-maze-binary-file/#t=139.38 |
| 02:24 | https://realpython.com/videos/python-maze-binary-file/#t=144.06 |
| Tools like the Unix file command leverage known magic numbers to detect file | https://realpython.com/videos/python-maze-binary-file/#t=144.06 |
| types. | https://realpython.com/videos/python-maze-binary-file/#t=148.41 |
| In this case, | https://realpython.com/videos/python-maze-binary-file/#t=149.893 |
| the tool correctly recognized an SVG graphics file | https://realpython.com/videos/python-maze-binary-file/#t=153.12 |
| 02:39 | https://realpython.com/videos/python-maze-binary-file/#t=159.18 |
| and the contents of a TOML file as ASCII text. | https://realpython.com/videos/python-maze-binary-file/#t=159.18 |
| 02:46 | https://realpython.com/videos/python-maze-binary-file/#t=166.28 |
| You can choose any magic number for your custom maze file format, | https://realpython.com/videos/python-maze-binary-file/#t=166.28 |
| but in this course you’ll use the four ASCII characters MAZE in uppercase. | https://realpython.com/videos/python-maze-binary-file/#t=169.48 |
| 02:54 | https://realpython.com/videos/python-maze-binary-file/#t=174.45 |
| The hexadecimal representation seen on screen looks less coffee-like, | https://realpython.com/videos/python-maze-binary-file/#t=174.45 |
| but it’s just as unique and recognizable. | https://realpython.com/videos/python-maze-binary-file/#t=178.85 |
| The magic number is usually followed by a file header, | https://realpython.com/videos/python-maze-binary-file/#t=181.95 |
| which provides useful metadata about the actual data in the file. | https://realpython.com/videos/python-maze-binary-file/#t=185.09 |
| 03:09 | https://realpython.com/videos/python-maze-binary-file/#t=189.68 |
| The header has a fixed structure consisting of fields, such as the width of the | https://realpython.com/videos/python-maze-binary-file/#t=189.68 |
| maze. | https://realpython.com/videos/python-maze-binary-file/#t=193.45 |
| However, the header may have a variable length—for example, | https://realpython.com/videos/python-maze-binary-file/#t=193.869 |
| due to enemy positions kept in the lookup table or other optional fields. | https://realpython.com/videos/python-maze-binary-file/#t=198.32 |
| 03:25 | https://realpython.com/videos/python-maze-binary-file/#t=205.14 |
| If you foresee introducing breaking changes to your file format in the future— | https://realpython.com/videos/python-maze-binary-file/#t=205.14 |
| for example, due to new features— | https://realpython.com/videos/python-maze-binary-file/#t=209.3 |
| then you should consider including a version field either at the beginning or | https://realpython.com/videos/python-maze-binary-file/#t=211.11 |
| before the header. | https://realpython.com/videos/python-maze-binary-file/#t=214.76 |
| 03:36 | https://realpython.com/videos/python-maze-binary-file/#t=216.77 |
| This should enable you to handle slight differences across file format versions. | https://realpython.com/videos/python-maze-binary-file/#t=216.77 |
| Each header field must have a known type, size, and offset measured in bytes since | https://realpython.com/videos/python-maze-binary-file/#t=223.61 |
| the beginning of the file, so you know where the next field begins. | https://realpython.com/videos/python-maze-binary-file/#t=228.34 |
| 03:52 | https://realpython.com/videos/python-maze-binary-file/#t=232.49 |
| The maze header file will have the three fields seen on-screen. | https://realpython.com/videos/python-maze-binary-file/#t=232.49 |
| The first field is a single byte with the file format version, | https://realpython.com/videos/python-maze-binary-file/#t=236.21 |
| which you may increment by one with each new release. | https://realpython.com/videos/python-maze-binary-file/#t=239.56 |
| 04:03 | https://realpython.com/videos/python-maze-binary-file/#t=243.33 |
| Notice that the version field starts with an offset of four bytes because the | https://realpython.com/videos/python-maze-binary-file/#t=243.33 |
| magic number occupies the first four bytes in the file. | https://realpython.com/videos/python-maze-binary-file/#t=246.95 |
| The other two fields are unsigned integers taking up four bytes each, | https://realpython.com/videos/python-maze-binary-file/#t=250.89 |
| which means they can have values between zero and over four billion, | https://realpython.com/videos/python-maze-binary-file/#t=254.19 |
| which should be enough for anybody. | https://realpython.com/videos/python-maze-binary-file/#t=257.68 |
| 04:22 | https://realpython.com/videos/python-maze-binary-file/#t=262.56 |
| Once you get through the header and have complete information about the amount | https://realpython.com/videos/python-maze-binary-file/#t=262.56 |
| and layout of the data, you may start reading the file body, | https://realpython.com/videos/python-maze-binary-file/#t=265.95 |
| which is the main part of the file. | https://realpython.com/videos/python-maze-binary-file/#t=269.29 |
| 04:31 | https://realpython.com/videos/python-maze-binary-file/#t=271.288 |
| It’s where you’ll find most of your data, | https://realpython.com/videos/python-maze-binary-file/#t=271.288 |
| and in this case, | https://realpython.com/videos/python-maze-binary-file/#t=273.92 |
| the body will be a very long one-dimensional array of numbers representing the | https://realpython.com/videos/python-maze-binary-file/#t=274.76 |
| various border patterns around the squares and their roles. | https://realpython.com/videos/python-maze-binary-file/#t=278.94 |
| 04:44 | https://realpython.com/videos/python-maze-binary-file/#t=284.95 |
| Now that you’ve seen how the maze data can be represented in a binary file, | https://realpython.com/videos/python-maze-binary-file/#t=284.95 |
| in the next section you’ll look into packing the data of the file body using as | https://realpython.com/videos/python-maze-binary-file/#t=288.47 |
| few bytes as possible. | https://realpython.com/videos/python-maze-binary-file/#t=292.43 |
| Become a Member | https://realpython.com/account/join/ |
| https://realpython.com/videos/python-maze-solver-part-2-overview/ |
| Overview | https://realpython.com/courses/python-maze-solver/ |
| https://realpython.com/videos/python-maze-data-alignment/ |
|
Mazes in Python: Build and Visualize 02:47
| https://realpython.com/videos/python-maze-solver-part-1-overview/ |
|
Check Out What You'll Make 02:39
| https://realpython.com/videos/python-maze-solver-project/ |
|
Lay the Groundwork 04:02
| https://realpython.com/videos/python-maze-solver-groundwork/ |
|
Scaffold the Project Structure 04:50
| https://realpython.com/lessons/python-maze-solver-structure/ |
|
Represent the Maze With Objects 06:37
| https://realpython.com/lessons/represent-maze-with-objects/ |
|
Create Border Patterns 06:32
| https://realpython.com/lessons/python-maze-border-patterns/ |
|
Model the Square 02:25
| https://realpython.com/lessons/model-python-maze-square/ |
|
Build the Maze 09:07
| https://realpython.com/lessons/build-python-maze/ |
|
Visualize the Maze With SVG 04:45
| https://realpython.com/lessons/python-maze-svg/ |
|
Implement Geometric Primitives 04:48
| https://realpython.com/lessons/python-maze-geometric-primitives/ |
|
Add More Primitives 03:47
| https://realpython.com/lessons/python-maze-more-primitives/ |
|
Decompose the Border Into Primitives 04:41
| https://realpython.com/lessons/decompose-python-maze-border/ |
|
Add More Border Patterns 03:58
| https://realpython.com/lessons/python-maze-more-border-patterns/ |
|
Build the SVG Renderer 03:49
| https://realpython.com/lessons/python-maze-svg-renderer/ |
|
Add Features to the Renderer 06:50
| https://realpython.com/lessons/python-maze-renderer-features/ |
|
Draw the Solution 03:13
| https://realpython.com/lessons/draw-python-maze-solution/ |
|
Preview the Rendered Maze and Its Solution 04:56
| https://realpython.com/lessons/preview-python-maze/ |
|
Mazes in Python: Build and Visualize (Recap) 01:14
| https://realpython.com/lessons/python-maze-solver-part-1-summary/ |
|
Mazes in Python: Store and Solve 01:48
| https://realpython.com/videos/python-maze-solver-part-2-overview/ |
|
Load the Maze From a Binary File 04:55
| https://realpython.com/videos/python-maze-binary-file/ |
|
Choose Your Data Alignment 05:09
| https://realpython.com/videos/python-maze-data-alignment/ |
|
Define the File Header and Body 08:12
| https://realpython.com/lessons/python-maze-file-header-and-body/ |
|
Serialize the Maze 05:22
| https://realpython.com/lessons/serialize-python-maze/ |
|
Deserialize the Maze 04:32
| https://realpython.com/lessons/deserialize-python-maze/ |
|
Make the Maze Self-Contained 04:30
| https://realpython.com/lessons/python-self-contained-maze/ |
|
Update the Maze Class 03:16
| https://realpython.com/lessons/python-maze-class/ |
|
Solve the Maze Using a Graph-Based Approach 03:01
| https://realpython.com/lessons/solve-python-maze-graph-based/ |
|
Transform the Maze Into a Graph 07:06
| https://realpython.com/lessons/transform-python-maze-graph/ |
|
Find the Shortest Path 05:23
| https://realpython.com/lessons/python-maze-shortest-path/ |
|
Add Weights to Graph Edges 02:36
| https://realpython.com/lessons/python-map-weights-graph-edges/ |
|
Introduce Enemies and Rewards 06:04
| https://realpython.com/lessons/python-maze-enemies-rewards/ |
|
Make a Runnable Script 03:59
| https://realpython.com/lessons/python-maze-runnable-script/ |
|
Mazes in Python: Store and Solve (Recap) 01:23
| https://realpython.com/lessons/python-maze-solver-part-2-summary/ |
| Privacy Policy | https://realpython.com/privacy-policy/ |
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover