# Some useful functions for reading/writing files
```python
# 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'
```
Raw data
{
"_id": null,
"home_page": "https://github.com/hansalemaos/write_read_file",
"name": "write-read-file",
"maintainer": "",
"docs_url": null,
"requires_python": "",
"maintainer_email": "",
"keywords": "files,read,write",
"author": "Johannes Fischer",
"author_email": "<aulasparticularesdealemaosp@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/62/aa/722f949e4accd466e0eabe7ee6d42188ba6d4e16fc82a9a893d3695a316d/write_read_file-0.10.tar.gz",
"platform": null,
"description": "\n# Some useful functions for reading/writing files \n\n\n```python\n# Tested with:\n# Python 3.9.13\n# Windows 10\n\nfrom write_read_file import write_utf8, write_bytes, read_bytes, copy_file, read_and_decode, iterread_bytes, iterread_text\n\nimport numpy as np\n\nalldata = [\n np.array([[1, 2, 3], [54, 56, 77]]),\n [10, 12, 44, 1, \"gjad\", \"\"],\n \"This is a text\",\n b\"this is a text\",\n {1: 22, 33: \"hithere\"},\n]\npath = \"f:\\\\test_write_file\\\\myfile.txt\"\npath2 = \"f:\\\\test_write_file\\\\myfile2.txt\"\n\nfor data in alldata:\n write_utf8(path, data, endofline=\"\\n\")\n print(\"\"\"iterread_text\"\"\")\n for line in iterread_text(\n path, encoding=\"utf-8\", strip_newline=True, ignore_empty_lines=True\n ):\n print(line)\n print(\"\"\"iterread_bytes\"\"\")\n\n for chunk in iterread_bytes(path, chunksize=128):\n print(chunk)\n print(\"\"\"write_bytes\"\"\")\n\n write_bytes(path, data=[str(x).encode() for x in data], endofline=b\"\\n\")\n print(\"\"\"iterread_bytes\"\"\")\n\n for chunk in iterread_bytes(path, chunksize=128):\n print(chunk)\n print(\"\"\"read_and_decode\"\"\")\n\n bytestostring = read_and_decode(\n path, decodeformat=\"utf-8\", on_encoding_errors=\"replace\"\n )\n print(bytestostring)\n print(\"\"\"copy_file\"\"\")\n\n copy_file(path, path2)\n print(\"\"\"iterread_bytes\"\"\")\n\n for chunk in iterread_bytes(path2, chunksize=128):\n print(chunk)\n print(\"\"\"read_bytes\"\"\")\n\n bytedata = read_bytes(path2)\n print(bytedata)\n\nOutput: \niterread_text\n1\n2\n3\n54\n56\n77\niterread_bytes\nb'1\\r\\n2\\r\\n3\\r\\n54\\r\\n56\\r\\n77\\r\\n'\nwrite_bytes\niterread_bytes\nb'[1 2 3]\\n[54 56 77]\\n'\nread_and_decode\n[1 2 3]\n[54 56 77]\ncopy_file\niterread_bytes\nb'[1 2 3]\\n[54 56 77]\\n'\nread_bytes\nb'[1 2 3]\\n[54 56 77]\\n'\niterread_text\n10\n12\n44\n1\ngjad\niterread_bytes\nb'10\\r\\n12\\r\\n44\\r\\n1\\r\\ngjad\\r\\n\\r\\n'\nwrite_bytes\niterread_bytes\nb'10\\n12\\n44\\n1\\ngjad\\n\\n'\nread_and_decode\n10\n12\n44\n1\ngjad\ncopy_file\niterread_bytes\nb'10\\n12\\n44\\n1\\ngjad\\n\\n'\nread_bytes\nb'10\\n12\\n44\\n1\\ngjad\\n\\n'\niterread_text\nThis is a text\niterread_bytes\nb'This is a text\\r\\n'\nwrite_bytes\niterread_bytes\nb'T\\nh\\ni\\ns\\n \\ni\\ns\\n \\na\\n \\nt\\ne\\nx\\nt\\n'\nread_and_decode\nT\nh\ni\ns\n \ni\ns\n \na\n \nt\ne\nx\nt\ncopy_file\niterread_bytes\nb'T\\nh\\ni\\ns\\n \\ni\\ns\\n \\na\\n \\nt\\ne\\nx\\nt\\n'\nread_bytes\nb'T\\nh\\ni\\ns\\n \\ni\\ns\\n \\na\\n \\nt\\ne\\nx\\nt\\n'\niterread_text\nb'this is a text'\niterread_bytes\nb\"b'this is a text'\\r\\n\"\nwrite_bytes\niterread_bytes\nb'116\\n104\\n105\\n115\\n32\\n105\\n115\\n32\\n97\\n32\\n116\\n101\\n120\\n116\\n'\nread_and_decode\n116\n104\n105\n115\n32\n105\n115\n32\n97\n32\n116\n101\n120\n116\ncopy_file\niterread_bytes\nb'116\\n104\\n105\\n115\\n32\\n105\\n115\\n32\\n97\\n32\\n116\\n101\\n120\\n116\\n'\nread_bytes\nb'116\\n104\\n105\\n115\\n32\\n105\\n115\\n32\\n97\\n32\\n116\\n101\\n120\\n116\\n'\niterread_text\n22\nhithere\niterread_bytes\nb'22\\r\\nhithere\\r\\n'\nwrite_bytes\niterread_bytes\nb'1\\n33\\n'\nread_and_decode\n1\n33\ncopy_file\niterread_bytes\nb'1\\n33\\n'\nread_bytes\nb'1\\n33\\n'\n\t\n\t\n```\n\n\n\n\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "Some useful functions for reading/writing files",
"version": "0.10",
"split_keywords": [
"files",
"read",
"write"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "ca8b2f83f495f690a6430b8b979a7aeae8ea7fe5e39dbef2702cf30dd4f81b98",
"md5": "118ff7ea5f4c416cfb0290e3264c1311",
"sha256": "c845cd5da80185f207343b6c0c4681f44894587665131fd89a9789e0fa7a2239"
},
"downloads": -1,
"filename": "write_read_file-0.10-py3-none-any.whl",
"has_sig": false,
"md5_digest": "118ff7ea5f4c416cfb0290e3264c1311",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 6107,
"upload_time": "2023-01-04T02:30:50",
"upload_time_iso_8601": "2023-01-04T02:30:50.549866Z",
"url": "https://files.pythonhosted.org/packages/ca/8b/2f83f495f690a6430b8b979a7aeae8ea7fe5e39dbef2702cf30dd4f81b98/write_read_file-0.10-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "62aa722f949e4accd466e0eabe7ee6d42188ba6d4e16fc82a9a893d3695a316d",
"md5": "6f2678c418ae2882b2a63fb2de6b3ca7",
"sha256": "613af8a8f8fe7c1e2f3d18634dfec6281db21e130ad5ba514f26a88d58b588b0"
},
"downloads": -1,
"filename": "write_read_file-0.10.tar.gz",
"has_sig": false,
"md5_digest": "6f2678c418ae2882b2a63fb2de6b3ca7",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 4981,
"upload_time": "2023-01-04T02:30:51",
"upload_time_iso_8601": "2023-01-04T02:30:51.848705Z",
"url": "https://files.pythonhosted.org/packages/62/aa/722f949e4accd466e0eabe7ee6d42188ba6d4e16fc82a9a893d3695a316d/write_read_file-0.10.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-01-04 02:30:51",
"github": true,
"gitlab": false,
"bitbucket": false,
"github_user": "hansalemaos",
"github_project": "write_read_file",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "flatten_any_dict_iterable_or_whatsoever",
"specs": []
},
{
"name": "isiter",
"specs": []
},
{
"name": "touchtouch",
"specs": []
}
],
"lcname": "write-read-file"
}