pythonfile

Wrapper of the file procedures to provide an interface as similar as possible to that of Python

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version Unknown
License MIT
CI Status Failing
Downloads 0
Last Indexed 2026-07-21 05:23

Installation

nimble install pythonfile
choosenim install pythonfile
git clone https://github.com/achesak/nim-pythonfile

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
pythonfile - - - - - - -

Source

Repository https://github.com/achesak/nim-pythonfile
Homepage https://github.com/achesak/nim-pythonfile
Registry Source nimble_official

README

About

nim-pythonfile is a Nim module to wrap the file functions and provide an interface as similar as possible to that of Python.

Examples:

# Open a file for reading, read and print one line, then read and store the next ten bytes.
var f: PythonFile = open("my_file.txt", "r")        # f = open("my_file.txt", "r")
echo(f.readline())                                  # print(f.readline())
var s: string = f.read(10)                          # s = f.read(10)
f.close()                                           # f.close()

# Open a file for writing, write "Hello World!", then write multiple lines at once.
var f: PythonFile = open("my_file.txt", "w")        # f = open("my_file.txt", "w")
f.write("Hello World!")                             # f.write("Hello World!")
f.writelines(["This", "is", "an", "example"])       # f.writelines(["This", "is", "an", "example"])
f.close()                                           # f.close()

# Open a file for reading or writing, then read and write from multiple locations
# using seek() and tell().
var f: PythonFile = open("my_file.txt", "r+")       # f = open("my_file.txt", "r+")
f.seek(10)                                          # f.seek(10)
echo(f.read())                                      # print(f.read())
echo(f.tell())                                      # print(f.tell())
f.seek(0)                                           # f.seek(0)
f.seek(-50, 2)                                      # f.seek(-50, 2)
f.write("Inserted at pos 50 from end")              # f.write("Inserted at pos 50 from end")
f.close()                                           # f.close()

# Basic with/as grammer for file operation
with open("temp.txt", "w+") as f:
    f.write("test")
    f.seek(0, 0)
    echo(f.read())

Note that due to some inherent differences between how Nim and Python handle files, a complete 1 to 1 wrapper is not possible. Notably, Nim has no equivalent to the newlines and encoding properties, and while they are present in this implementation they are always set to empty string. In addition, the fileno() procedure functions differently from how it does in Python, yet it has the same basic functionality.

For general use, however, this wrapper provides all of the common Python file methods.

License

nim-pythonfile is released under the MIT open source license.