How to Close a File in Python

Close a file in Python: Command Prompt output of close() setting the closed attribute to True

To close a file in Python, call close() on the file object, or use a with block, which closes it for you even after an error: My machine for these runs is Python 3.12.5 on Windows 11, which matters for the Windows lock section. In this tutorial, we’ll look at close(), the closed attribute, flush … Read more >>

How to Open a File in Python (Modes, Encoding, Paths)

How to open a file in Python: Command Prompt output of open() in x, a, r and w modes

You open a file in Python with the built-in open() function: pass it the file path, a mode such as “r” or “w”, and an encoding. Wrap it in a with block so Python closes the file for you, even when an error happens: My test machine for this page runs Python 3.12.5 on Windows … Read more >>

How to Add Days to a Date in TypeScript (setDate and DST)

TypeScript add days to date: Command Prompt comparing setDate with adding 86400000 ms across daylight saving time

Copy the Date, then call setDate(getDate() + days) on the copy: that’s how you add days to a date in TypeScript. JavaScript rolls the month and year over for you, and negative numbers go back in time: Every result here came from TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, with the time zone set … Read more >>

How to Add Seconds to a Date in TypeScript

TypeScript add seconds to date: Command Prompt output of an addSeconds helper using getTime

The cleanest way to add seconds to a date in TypeScript is to read the timestamp with getTime(), add the seconds times 1000, and wrap the sum in a new Date. Your original date isn’t touched: I ran every snippet here with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so the printed results are … Read more >>

How to Check Array Equality in TypeScript (5 Ways)

TypeScript array equality: Command Prompt output of a deepEqual check on two arrays of objects

To check array equality in TypeScript, compare the lengths and then every element by index. === on two arrays only tells you whether they’re the same object, so build a small typescript array equality helper: Strict mode was on for all of these snippets, with TypeScript 7.0.2 and Node.js v22.22.2 doing the work, so what … 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 >>

How to Convert a Set to an Array in TypeScript

Convert a Set to an array in TypeScript: Command Prompt output of turning a Set of orders into a sorted array

Spread syntax is the quickest way to convert a Set to an array in TypeScript: […mySet]. Array.from(mySet) does the same job. Both return a new, typed array in insertion order, so a Set<string> becomes a string[]: I compiled and ran every example below with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, and the outputs … Read more >>

How to Concatenate Arrays in TypeScript Using concat()

Concatenate arrays in TypeScript: Command Prompt output of joining two order arrays with concat()

concat() is the built-in way to concatenate arrays in TypeScript: a.concat(b) returns a new array with every item of a followed by every item of b, and neither original changes. The spread syntax […a, …b] does the same job: Every snippet was compiled and run with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so … Read more >>

How to Find the Length of an Array in TypeScript

TypeScript array length: Command Prompt output of counting orders with filter().length

To find the length of an array in TypeScript, read its length property: arr.length returns the number of elements as a number, and an empty array gives 0. There’s no size() or count() method on arrays: I ran every snippet with TypeScript 7.0.2 and Node.js v22.22.2 in strict mode, so the output you see is … Read more >>