chiphuyen / stanford-tensorflow-tutorials

This repository contains code examples for the Stanford's course: TensorFlow for Deep Learning Research.
http://cs20.stanford.edu
MIT License
10.32k stars 4.32k forks source link

ValueError: Must have exactly one of create/read/write/append mode #73

Closed mohd7469 closed 6 years ago

mohd7469 commented 6 years ago

after changing all 'rb' and 'bw' to 'r' and 'b' it gives the following error

File "data.py", line 76, in prepare_dataset files.append(open(os.path.join(config.PROCESSED_PATH, filename),'b')) ValueError: Must have exactly one of create/read/write/append mode and at most one plus

does anybody knows? what to do

MartinAbilev commented 6 years ago

why still there is 'b' ?? use just r and w

for filename in filenames:
    files.append(open(os.path.join(config.PROCESSED_PATH, filename),'w'))

or place a+

mohd7469 commented 6 years ago

on replacing 'b' with 'a+'

Traceback (most recent call last):
  File "data.py", line 250, in <module>
    prepare_raw_data()
  File "data.py", line 176, in prepare_raw_data
    prepare_dataset(questions, answers)
  File "data.py", line 78, in prepare_dataset
    files[0].write(questions[i] + '\n')
  File "C:\Users\APPBAKERZ-PC\AppData\Local\Programs\Python\Python35\lib\encodin
gs\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\x97' in position 16
8: character maps to <undefined>

with 'w'

Traceback (most recent call last):
  File "data.py", line 250, in <module>
    prepare_raw_data()
  File "data.py", line 176, in prepare_raw_data
    prepare_dataset(questions, answers)
  File "data.py", line 78, in prepare_dataset
    files[0].write(questions[i] + '\n')
  File "C:\Users\APPBAKERZ-PC\AppData\Local\Programs\Python\Python35\lib\encodin
gs\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\x97' in position 16
8: character maps to <undefined>

with 'r'

Traceback (most recent call last):
  File "data.py", line 250, in <module>
    prepare_raw_data()
  File "data.py", line 176, in prepare_raw_data
    prepare_dataset(questions, answers)
  File "data.py", line 78, in prepare_dataset
    files[0].write(questions[i] + '\n')
io.UnsupportedOperation: not writable
sushant097 commented 6 years ago

As, i changed as, files.append(open(os.path.join(config.PROCESSED_PATH, filename), 'w')) and it work. While opening file you should change rb and wb to r and b . That's all!

MartinAbilev commented 6 years ago

Normally, files are opened in text mode, that means, you read and write strings from and to the file, which are encoded in a specific encoding. If encoding is not specified, the default is platform dependent (see open()). 'b' appended to the mode opens the file in binary mode: now the data is read and written in the form of bytes objects. This mode should be used for all files that don’t contain text. As i remember there no binary files used in this example