How to Move a File in Python (shutil and pathlib)

Move a file in Python: Command Prompt output of shutil.move moving an invoice into an archive folder

The easiest way to move a file in Python is shutil.move(source, destination). Give it a folder and the file goes inside it; give it a full path and the file is moved and renamed in one step: These examples created, moved and checked real files with Python 3.12.5 on Windows; the screenshots show each script’s … Read more >>

Python Import from Another Directory (5 Ways That Work)

Python import from another directory: ModuleNotFoundError fixed by running the app with python -m

For a Python import from another directory, the cleanest fix is to make the folder a package and run your script as a module from the project root with python -m. Python then finds sibling folders automatically: I built that small project for real and ran every command with Python 3.12.5 in Command Prompt, errors … Read more >>

os.path.join() in Python: Join Paths the Right Way

os path join in Python: Command Prompt output of os.path.join with absolute paths and drive letters

os.path.join() in Python combines folder and file names into one path, using the right separator for your operating system: a backslash on Windows and a forward slash on macOS and Linux. My machine runs Python 3.12.5 on Windows, so the screenshots show backslashes; I also use posixpath to show what macOS and Linux would print. … Read more >>

loc vs iloc in pandas: What’s the Difference?

Pandas DataFrame loc vs iloc: Command Prompt output of label-based and position-based slicing

In a pandas DataFrame, loc vs iloc comes down to labels vs positions: loc selects rows and columns by their labels (index names, column names), while iloc selects by integer position, starting at 0. The output and screenshots in this guide come from Python 3.12.5 with pandas 3.0.6, using a small table of sales for … Read more >>

Order of Operations in Python (Operator Precedence)

Order of operations in Python: Command Prompt output showing PEMDAS rules and parentheses

Python’s order of operations follows the PEMDAS rule from math class: parentheses first, then exponents (**), then multiplication and division, then addition and subtraction, working left to right within each level. I checked every expression on Python 3.12.5, so the results below are exactly what Python prints, including a few that surprise people. Below you’ll … Read more >>

Breadth First Search in Python (BFS with Examples)

Breadth first search in Python: Command Prompt output of BFS solving a grid maze with the shortest path

Breadth first search in Python explores a graph level by level: first the start node, then all its neighbors, then their neighbors, and so on. You build it with a queue from collections.deque and a set of visited nodes: Every BFS here uses only the standard library, and I ran each one with Python 3.12.5 … Read more >>

How to Format Currency to 2 Decimals in TypeScript

Format currency to 2 decimals in TypeScript: Command Prompt output of Intl.NumberFormat with USD

You can format currency to 2 decimals in TypeScript with Intl.NumberFormat: pass style: ‘currency’ and currency: ‘USD’, and you get a string like $1,234.50 with the symbol, commas and two decimals: Every output on this page came from a real run with TypeScript 7.0.2 and Node.js v22.22.2, strict mode on. Format currency to 2 decimals … Read more >>

pip install -r requirements.txt: Install, Create and Fix Errors

pip install from requirements txt: Command Prompt installing packages into a virtual environment

Running pip install from a requirements txt file takes one command: activate your virtual environment in the project folder, then run pip install -r requirements.txt. pip reads every line and installs those packages and versions: Every command here really ran on Python 3.12.5 in Windows Command Prompt, installing two small packages from PyPI into a … Read more >>

How to Make a Directory in Python (os and pathlib)

Make a directory in Python: Command Prompt output of pathlib mkdir creating nested folders

Need to make a directory in Python? Use os.mkdir(“name”) for a single folder, or Path(“a/b/c”).mkdir(parents=True, exist_ok=True) to create nested folders without errors if they already exist: Each folder in these examples was created for real by Python 3.12.5 on Windows, and the screenshots show the Command Prompt output. I’ll cover os.mkdir(), os.makedirs(), pathlib, dated and … Read more >>

How to Convert Number to Boolean in TypeScript

Convert number to boolean in TypeScript: Command Prompt output of Boolean(), !! and n !== 0 for 0, -0, NaN and Infinity

The quickest way to convert number to boolean in TypeScript is Boolean(n) or !!n. Both return false for 0, -0 and NaN, and true for every other number, including negatives and Infinity: I ran each snippet below with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so the console output you see is real. We’ll … Read more >>