Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

An integer is required? open()

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?

like image 811
Victor Avatar asked Jun 25 '09 23:06

Victor


People also ask

Why do we need Io open () in Python?

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):

What does the integer error in Photoshop mean?

Error while accessing performance preferences - 'An integer between 96 and 8 is required.' Learn how to resolve the integer error in Photoshop. This error occurs when you try to access the Performance preferences.

When is a set U ⊂ R open?

A set U ⊂ R is open if and only if for every x ∈ U, there exists some ϵ > 0 such that ( x − ϵ, x + ϵ) is a subset of U. Take any ϵ > 0. Then, min { x + ϵ 2, x + 1 2 } is an element of ( x − ϵ, x + ϵ), but it is not an element of Z. Therefore, Z is not open.

How to use OS Open () instead of OS Open?

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.


1 Answers

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.

like image 138
Mark Rushakoff Avatar answered Sep 28 '22 01:09

Mark Rushakoff