Skip to main content

Some useful functions for reading/writing files

# Tested with:
# Python 3.9.13
# Windows 10

from write_read_file import write_utf8, write_bytes, read_bytes, copy_file, read_and_decode, iterread_bytes, iterread_text

import numpy as np

alldata = [
    np.array([[1, 2, 3], [54, 56, 77]]),
    [10, 12, 44, 1, "gjad", ""],
    "This is a text",
    b"this is a text",
    {1: 22, 33: "hithere"},
]
path = "f:\\test_write_file\\myfile.txt"
path2 = "f:\\test_write_file\\myfile2.txt"

for data in alldata:
    write_utf8(path, data, endofline="\n")
    print("""iterread_text""")
    for line in iterread_text(
        path, encoding="utf-8", strip_newline=True, ignore_empty_lines=True
    ):
        print(line)
    print("""iterread_bytes""")

    for chunk in iterread_bytes(path, chunksize=128):
        print(chunk)
    print("""write_bytes""")

    write_bytes(path, data=[str(x).encode() for x in data], endofline=b"\n")
    print("""iterread_bytes""")

    for chunk in iterread_bytes(path, chunksize=128):
        print(chunk)
    print("""read_and_decode""")

    bytestostring = read_and_decode(
        path, decodeformat="utf-8", on_encoding_errors="replace"
    )
    print(bytestostring)
    print("""copy_file""")

    copy_file(path, path2)
    print("""iterread_bytes""")

    for chunk in iterread_bytes(path2, chunksize=128):
        print(chunk)
    print("""read_bytes""")

    bytedata = read_bytes(path2)
    print(bytedata)

Output: 
iterread_text
1
2
3
54
56
77
iterread_bytes
b'1\r\n2\r\n3\r\n54\r\n56\r\n77\r\n'
write_bytes
iterread_bytes
b'[1 2 3]\n[54 56 77]\n'
read_and_decode
[1 2 3]
[54 56 77]
copy_file
iterread_bytes
b'[1 2 3]\n[54 56 77]\n'
read_bytes
b'[1 2 3]\n[54 56 77]\n'
iterread_text
10
12
44
1
gjad
iterread_bytes
b'10\r\n12\r\n44\r\n1\r\ngjad\r\n\r\n'
write_bytes
iterread_bytes
b'10\n12\n44\n1\ngjad\n\n'
read_and_decode
10
12
44
1
gjad
copy_file
iterread_bytes
b'10\n12\n44\n1\ngjad\n\n'
read_bytes
b'10\n12\n44\n1\ngjad\n\n'
iterread_text
This is a text
iterread_bytes
b'This is a text\r\n'
write_bytes
iterread_bytes
b'T\nh\ni\ns\n \ni\ns\n \na\n \nt\ne\nx\nt\n'
read_and_decode
T
h
i
s
 
i
s
 
a
 
t
e
x
t
copy_file
iterread_bytes
b'T\nh\ni\ns\n \ni\ns\n \na\n \nt\ne\nx\nt\n'
read_bytes
b'T\nh\ni\ns\n \ni\ns\n \na\n \nt\ne\nx\nt\n'
iterread_text
b'this is a text'
iterread_bytes
b"b'this is a text'\r\n"
write_bytes
iterread_bytes
b'116\n104\n105\n115\n32\n105\n115\n32\n97\n32\n116\n101\n120\n116\n'
read_and_decode
116
104
105
115
32
105
115
32
97
32
116
101
120
116
copy_file
iterread_bytes
b'116\n104\n105\n115\n32\n105\n115\n32\n97\n32\n116\n101\n120\n116\n'
read_bytes
b'116\n104\n105\n115\n32\n105\n115\n32\n97\n32\n116\n101\n120\n116\n'
iterread_text
22
hithere
iterread_bytes
b'22\r\nhithere\r\n'
write_bytes
iterread_bytes
b'1\n33\n'
read_and_decode
1
33
copy_file
iterread_bytes
b'1\n33\n'
read_bytes
b'1\n33\n'
	
	

Metadata

Release files for write-read-file 0.10

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for write-read-file 0.10
File Size Uploaded Provenance Actions
write_read_file-0.10.tar.gz 5.0 kB Without provenance Details for write_read_file-0.10.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for write-read-file 0.10
File Interpreter ABI Platform Actions
write_read_file-0.10-py3-none-any.whl Python 3 none any Details for write_read_file-0.10-py3-none-any.whl

Total release size: 11.1 kB

Release files / write_read_file-0.10.tar.gz

Download URL write_read_file-0.10.tar.gz
Size 5.0 kB
Tags Source
SHA-256 checksum
How to use checksums
613af8a8f8fe7c1e2f3d18634dfec6281db21e130ad5ba514f26a88d58b588b0
BLAKE2b-256 checksum
How to use checksums
62aa722f949e4accd466e0eabe7ee6d42188ba6d4e16fc82a9a893d3695a316d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / write_read_file-0.10-py3-none-any.whl

Download URL write_read_file-0.10-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c845cd5da80185f207343b6c0c4681f44894587665131fd89a9789e0fa7a2239
BLAKE2b-256 checksum
How to use checksums
ca8b2f83f495f690a6430b8b979a7aeae8ea7fe5e39dbef2702cf30dd4f81b98
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for write-read-file 0.10

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.10 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page