This is a trick that can be used for testing and debugging. Want to create a large file with random content for testing? .. Moreover, it is instant.
Execution example (creating a 1024-byte file)
C:\>python -c "import sys; import numpy; sys.stdout.buffer.write(numpy.random.bytes(1024));" > 1024.bin
Execution example (1MB/1,048,576 byte file creation)
C:\>python -c "import sys; import numpy; sys.stdout.buffer.write(numpy.random.bytes(1048576));" > 1048576.bin
Execution example (1MB/1,073,741,824-byte file creation)
python -c "import sys; import numpy; sys.stdout.buffer.write(numpy.random.bytes(1073741824));" > 1073741824.bin
It took about 1 minute to create a 1GB file with Core i7-5500U-2.4GHz/SSD-DISK. (If you want a zero-padded file, please use fsutil. It will be finished in an instant)
Execution result
C:\work>dir
2021/01/11 10:00 1,024 1024.bin
2021/01/11 10:08 1,048,576 1048576.bin
2021/01/12 19:28 1,073,741,824 1073741824.bin
I posted a method to realize it instantly only on the command line. I hope it helps you when you are busy.
>>> sys.stdout.write(numpy.random.bytes(1024))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: write() argument must be str, not bytes
I got an error when I tried to output. The part-time job is useless. I thought it was okay because numpy.random.bytes is returned as a string type like "b'\ xe5'". ..
C:\>python
>>> import sys;
>>> print(type(sys.stdout));
<class '_io.TextIOWrapper'>
The type of sys.stdout is'_io.TextIOWrapper'.
────────────────────────── class '_io.TextIOWrapper' A buffered text stream providing higher-level access to a BufferedIOBase buffered binary stream. It inherits TextIOBase. (** Inherit TextIOBase **) ────────────────────────── '_io.TextIOWrapper' was a class for text processing. ** I have no choice but to look for a byte interface **. It is said to inherit'TextIOBase'.
────────────────────────── class io.TextIOBase Base class for text streams. This class provides a character and line based interface to stream I/O. It inherits IOBase. There is no public constructor. ────────────────────────── This class had a member called buffer that accessed the binary IF.
────────────────────────── buffer The underlying binary buffer (BufferedIOBase instance) that TextIOBase handles. ────────────────────────── It seems that you can use buffer for binary output.
Recommended Posts