I have a very simple python script that should scan a text file, which contains lines formatted as id=\'value\' and put them into a dict.
Also of note is that starting with Python 2.6 the built-in function open() is now an alias for the io.open() function. It was even considered removing the built-in open() in Python 3 and requiring the usage of io.open, in order to avoid accidental namespace collisions resulting from things such as "from blah import *". In Python 2.6+ you can write (and can also consider this style to be good practice):
import io
filehandle = io.open(sys.argv[1], 'r')
Don't do import * from wherever
without a good reason (and there aren't many).
Your code is picking up the os.open() function instead of the built-in open() function. If you really want to use os.open(), do import os
then call os.open(....)
. Whichever open you want to call, read the documentation about what arguments it requires.
From http://www.tutorialspoint.com/python/os_open.htm you could also keep your import and use
file = os.open( "foo.txt", mode )
and the mode could be :
os.O_RDONLY: open for reading only
os.O_WRONLY: open for writing only
os.O_RDWR : open for reading and writing
os.O_NONBLOCK: do not block on open
os.O_APPEND: append on each write
os.O_CREAT: create file if it does not exist
os.O_TRUNC: truncate size to 0
os.O_EXCL: error if create and file exists
os.O_SHLOCK: atomically obtain a shared lock
os.O_EXLOCK: atomically obtain an exclusive lock
os.O_DIRECT: eliminate or reduce cache effects
os.O_FSYNC : synchronous writes
os.O_NOFOLLOW: do not follow symlinks
Because you did from os import *
, you are (accidenally) using os.open, which indeed requires an integer flag instead of a textual "r" or "w". Take out that line and you'll get past that error.
Providing these parameters resolved my issue:
with open('tomorrow.txt', mode='w', encoding='UTF-8', errors='strict', buffering=1) as file:
file.write(result)