Have you ever been asked or wonder how to learning a programming language? If not, then you can skip this blog post. :-)
At work, a coworker said to me “I want to learn a programming language. Where do I start or how do I even start it?” I was asked this question because I had explicitly expressed my interest to my teams that I like programming a lot, though programming is not being used my professional career at the moment.
Well, so what did I tell my colleague? I replied, “Personally, I’d think of a project which I want to write or code for. Then start to code it.” Sure, it’ll be very hard if you don’t know any programming language at all. But nowadays, if you have free time, there are so many great contents on Youtube and online teaching programming. You can watch those contents to understand the basic and then apply and enforece your understanding with your project.
So, today, to practice what I preach, I decided to think of a tiny project or task which I can make use of Python programming. Sometimes, it’s hard to think of something to work on, I know. Fortunately, it was not my case.
On my Windows machine, I use a program called Hugo to generate static site (including this blog). Whenever there is a new version of Hugo available, I have to manually visit Github page of Hugo project, download the zip archived binary, extract it and finally copy/move the binary to the right location. It’s not fun, and I am not into using the Terminal on Windows and PowerShell much. What would be fun then? Write a Python script to extract the hugo.exe
file from the downloaded zip archive and copy it to the desired location, (e.g.C:\Users\kenno\apps\hugo.exe
).
Here is the whole working Python code.
import zipfile
from pathlib import Path
downloads_path = str(Path.home() / "Downloads")
extracted_path = f"{Path.home()}\\apps"
downloaded_file = "hugo_extended_0.118.2_windows-amd64.zip"
desired_extract_file = "hugo.exe"
print(f"\nUpdating '{desired_extract_file}' from '{downloaded_file}':")
# Extract only a file named 'hugo' (ignore other files such as README).
with zipfile.ZipFile(f"{downloads_path}\\{downloaded_file}", 'r') as zip:
#zip.printdir() # shown content of the archive
zip.extract(desired_extract_file, extracted_path)
print(f"File has been extracted to '{extracted_path}\{desired_extract_file}'\n")
On a Windows 11 machine with Python 3 installed, running the above script will produce the following output:
PS C:\Users\kenno\Documents\dev\python> python3 .\update-hugo.py
Updating 'hugo.exe' from 'hugo_extended_0.118.2_windows-amd64.zip':
File has been extracted to 'C:\Users\kenno\apps\hugo.exe'
To verify that the file has been copied to the desired location:
PS C:\Users\kenno\Documents\dev\python> Get-Item C:\Users\kenno\apps\hugo.exe
Directory: C:\Users\kenno\apps
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a---- 9/27/2023 12:15 AM 68660736 hugo.exe
To recap, if you want to learn a programming language, after learning the fundamental of the language itself by consuming YYoutube content or online tutorials, try to think of a (small or big) project and work on it.