It's not limited to python, but ** I hate file operations too much **, so I'll try file operations a little. File operations here are at the level of opening, writing, and closing files.
For the time being, I think it will take some time for a third party to make the article meaningful.
step1
I ran the following code.
file_step1.py
f1 = open('testfile1.txt')
f1.write(123)
f1.write(456)
f1.close
I was scolded as follows.
C:\_temp_work\fileA>python file_step1.py
Traceback (most recent call last):
File "file_step1.py", line 2, in <module>
f1.write(123)
TypeError: write() argument must be str, not int
C:\_temp_work\fileA>
The above is an extra error, but I corrected the error.
file_step1a.py
f1 = open('testfile1.txt')
f1.write('123')
f1.write('456')
f1.close
Also, I was scolded.
C:\_temp_work\fileA>python file_step1a.py
Traceback (most recent call last):
File "file_step1a.py", line 2, in <module>
f1.write('123')
io.UnsupportedOperation: not writable
C:\_temp_work\fileA>
Fixed.
file_step1b.py
f1 = open('testfile1.txt', mode='w')
f1.write('123')
f1.write('456')
f1.close
This time, there is no problem.
C:\_temp_work\fileA>python file_step1b.py
C:\_temp_work\fileA>
testfile1.txt Can be done.
The contents are
testfile1.txt
123456
no problem.
Now that I'm interested in binaries ** I tried mode ='wb'. ** **
file_step2.py
f1 = open('testfile1.txt', mode='wb')
f1.write('123')
f1.write('456')
f1.close
Also, I was scolded.
C:\_temp_work\fileA>python file_step2.py
Traceback (most recent call last):
File "file_step2.py", line 2, in <module>
f1.write('123')
TypeError: a bytes-like object is required, not 'str'
C:\_temp_work\fileA>
to be continued.