Write Text And Populate a New File From Template with Python

usecase

The concern is documenting patterns for writing into a python file, in particular to:

  1. create an empty markdown file with the title from an input
  2. populate with a template text

1. open()

  • even though you don’t have to pass selector, it is recommended for the sake of readability
  • as far as the write –vs– append goes:
  • the write mode erases the previous file data
  • the append mode attaches the content to the file
  • also, it is highly recommended to pass encoding=utf-8 (or any other desired one)

2. write()

  • the write() method writes a string to a stream
  • in a repl, the write() returns the number of code points (chars) written to the file
  • you are responsible for providing newline — where is no write line method on Python file objects
f = open('hello-world.txt', mode = 'wt', encoding = 'utf-8')
f.write('hello-world
')
f.write('hello-world, again!)
f.close()
  • which results in hello-world.txt with - even when done repeatedly (append would keep lines growing)
hello-world
hello-world, again!

3. usecase: populate a new file with a template for documentation

  • The script creates a file with the title based on the user’s input
  • Then it needs to populate it with markdown’s template
## Overview
The aim of this document is
### Use cases* common usage or referral tickets### Prerequisites* technical or knowledge
<!-- ... -->

3.1. test

  • I don’t know how to test for this side-effect, the fast/stupid way seems to be to check if the first line is identical to the one from the template (I guess you could count the number of lines or compare files, but this is just to learn new methods)
  • using the with-block
def test_template():
opsdocs.new_doc()
with open('./Demo/hello-world.md', mode='rt', encoding='utf-8') as template:
first_line = template.readline().strip()
assert first_line == "## Overview"

3.2. prod

  • not using the with-block
  • this is a private function within the larger function
def _createFile(docPath):
newDoc = open(docPath, mode='wt', encoding='utf-8')
template = open('./zendesk/template.md', mode='rt', encoding='utf-8')
newDoc.write(template.read())
template.close()
newDoc.close()

4. sources

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

https://youtu.be/gyeO45SZPjs

Introduction to Package Management in Linux

My journey to cloud computing part 2

Knowing when to put code into your API/microservice vs. outside API (wrapper microservice)

3 Things Why You Should Not Become A Software Engineer

Reactive Programming: What & Why? — Part II

Setting the Tone

Deep Dive into Python Class Decorator

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

More from Medium

How To Attach Debugger To Python Script Called From Terminal In Visual Studio Code

Python virtual environments & libraries

How to Use UTF-8 with Python

Python ternary operator