Python logoPython/
PYL-W1501

Bad open mode for filePYL-W1501

Major severityMajor
Bug Risk categoryBug Risk

The mode with which the file is opened is not valid. Python supports: r, w, a, and x modes with t and b modifiers, for text files and binary files respectively. The + mode can also be used to open the file for updating (reading and writing).

Bad practice

with open('log.txt', 'rw') as logfile:  # Doesn't work
    text = logfile.read()
    logfile.write('test log')
with open('log.txt', 'r+') as logfile:
    text = logfile.read()
    logfile.write('test log')

References: