An integer is required? open()
Asked Answered
T

7

53

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. the python module is called chval.py and the input file is in.txt. here's the code:

import os,sys
from os import *
from sys import *

vals = {}

f = open(sys.argv[1], 'r')

for line in val_f:
    t = line.split('=')
    t[1].strip('\'')
    vals.append(t[0], t[1])

print vals

f.close()

when i try to run it i get:

Traceback (most recent call last):
File "chval.py", line 9, in ? f = open(sys.argv[1], 'r') TypeError: an integer is required

I'm using python 2.4... because i've been challenged to not use anything newer, is there something about open() that I don't know about? Why does it want an integer?

anything after that line is untested. in short: why is it giving me the error and how do i fix it?

Tabu answered 25/6, 2009 at 23:11 Comment(6)
Your script has a bug in it. vals is a dictionary, which does not have an append method. You want to just assign with vals[t[0]]=t[1]Decanter
also, it's supposed to be "for line in f:" as opposed to val_fGwinn
yeah sev, i had gone through and changed my variable halfway through... thanks for pointing that out.Tabu
so i had added another issue to my problem where i was getting a "file not found error" the machine i'm on at work is set to hide extensions so the file was called in.txt.txt instead of just in.txtTabu
cause == "machine ... is set to hide extensions"; effect = "the file was called in.txt.txt"???Kinsey
yeah my comment was unclear, i was originally getting an error that the file in.txt was did not exist, but in my mind "that's impossible!" so i started throwing things into my code. one of which was "from os import *" that brought up a different error and i assumed i had fixed one problem to find another. after this question was answered my code reverted back to the original error. So I edited my question to address the issue, upon finding that my machine was not showing extensions and that in.txt indeed did not exist i re-edited my question and left my previous comment in case it was read.Tabu
A
92

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.

Aurar answered 25/6, 2009 at 23:16 Comment(1)
Thank you so much. I was searching for the solution for so long. Finally could understand, where I overlook for so long. Thank you so much once again.Geminian
K
15

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.

Kinsey answered 25/6, 2009 at 23:31 Comment(0)
S
12

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')
Spaceman answered 26/6, 2009 at 0:10 Comment(0)
R
6

Providing these parameters resolved my issue:

with open('tomorrow.txt', mode='w', encoding='UTF-8', errors='strict', buffering=1) as file:
    file.write(result)
Rumney answered 6/11, 2017 at 5:44 Comment(0)
I
0

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
Ita answered 18/12, 2015 at 10:34 Comment(0)
D
0

that's because you should do:

open(sys.argv[2], "w", encoding="utf-8")

or

open(sys.argv[2], "w")
Dodger answered 11/9, 2021 at 6:30 Comment(0)
S
0

you have from os import * I also got the same error, remove that line and change it to import os and behind the os lib functions, add os.[function]

Selfcontained answered 13/10, 2021 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.