Reading a file using a relative path in a Python project

Relative paths are relative to current working directory. If you do not want your path to be relative, it must be absolute. But there is an often used trick to build an absolute path from current script: use its __file__ special attribute: from pathlib import Path path = Path(__file__).parent / “../data/test.csv” with path.open() as f: …

Read more

Binary buffer in Python

You are probably looking for io.BytesIO class. It works exactly like StringIO except that it supports binary data: from io import BytesIO bio = BytesIO(b”some initial binary data: \x00\x01″) StringIO will throw TypeError: from io import StringIO sio = StringIO(b”some initial binary data: \x00\x01″)

Asynchronous IO in Scala with futures

Use Futures in Scala 2.10. They were joint work between the Scala team, the Akka team, and Twitter to reach a more standardized future API and implementation for use across frameworks. We just published a guide at: http://docs.scala-lang.org/overviews/core/futures.html Beyond being completely non-blocking (by default, though we provide the ability to do managed blocking operations) and …

Read more

Reading two text files line by line simultaneously

with open(“textfile1”) as textfile1, open(“textfile2″) as textfile2: for x, y in izip(textfile1, textfile2): x = x.strip() y = y.strip() print(f”{x}\t{y}”) In Python 2, replace built-in zip with itertools.izip: from itertools import izip with open(“textfile1”) as textfile1, open(“textfile2”) as textfile2: for x, y in izip(textfile1, textfile2): x = x.strip() y = y.strip() print(“{0}\t{1}”.format(x, y))