0

I have to work with a txt file and to do that I used the following code:

inputFile = open("C:/Abaqus_JOBS/Job-M1-3_4.inp", "r") #CAE INPUT FILE

However I get this error when I ran this line in a specific application for running python scripts available in another program. I don't get any error when I ran it in Spyder.

TypeError: an integer is required

I don't have a clue why this error occurs....

EDIT: lines of code until line in question

import os
from os import *
from abaqus import *
from odbAccess import *
from abaqusConstants import *
import time
import itertools

os.chdir('C:\\Abaqus_JOBS')

LCKf = 'C:\\Abaqus_JOBS\\Job-M1-3_2.lck'
STAf = 'C:\\Abaqus_JOBS\\Job-M1-3_2.sta'

def get_num_part(s):
    for i in xrange(len(s)):
        if s[i:].isdigit():
            return s[i:]
    return ''

if not path.exists(LCKf):
    time.sleep(1)
while path.exists(LCKf) and path.isfile(LCKf) and access(LCKf, R_OK):
    variableX = 0
else:
    odb = openOdb(path='Job-M1-3_2.odb')
#get CF
#session.odbs[name].steps[name].frames[i].FieldOutput
    myAssembly = odb.rootAssembly    
    myAssemblyName = odb.rootAssembly.name

    nsteps=len(odb.steps.values())

    step1 = odb.steps.values()[nsteps-1]
    step1Name = odb.steps.values()[nsteps-1].name

    myInstanceName = odb.rootAssembly.instances.values()[0].name

    dCF3=[]
    dCF3v=[]
    coordFv=[]
    fileData = [] #array with the input file
    nodes = [] #array with the content of *NODES

    inputFile = open("C:/Abaqus_JOBS/Job-M1-3_4.inp", "r") #CAE INPUT FILE
    #fileData = variable with all the lines of the inp file
    for line in inputFile:
        fileData.append([x.strip() for x in line.split(',')])

the error is:

Traceback (most recent call last):
  File "c:/Abaqus_JOBS/results.py", line 47, in <module>
    inputFile = open("C:/Abaqus_JOBS/Job-M1-3_4.inp", "r") #CAE INPUT FILE
TypeError: an integer is required
7
  • 2
    Please give your full stack trace, and all the code you ran - it's highly likely the error is not happening on this line. (As a side note, when opening files it's best to use the with statement.) Commented May 6, 2013 at 16:22
  • 2
    Which application? This error obviously was not created by CPython on the line you gave, so the specific application is important. Commented May 6, 2013 at 16:23
  • 2
    You didn't do anything to shadow the builtin open did you? Commented May 6, 2013 at 16:23
  • 4
    Those from ... import * lines are a massive red-flag. It's likely one of them has an open() function that is shadowing the built-in. It's extremely rare that import * is a good idea - you should only do it occasionally with libraries specifically designed to be loaded like that. Commented May 6, 2013 at 16:26
  • 2
    Ah, good old os.open.. Commented May 6, 2013 at 16:35

1 Answer 1

3

With the

from os import *

You're importing all os stuff in the global namespace, including os.open(). Don't do this.

The second argument, flags, is defined as integer constants while you're providing a single-character string r. This is basically what DSM was telling you and what Lattyware said.

open() included in Python by default in the global namespace, which you were expecting apparently, is different:

Note: This function is intended for low-level I/O. For normal usage, use the built-in function open(), which returns a “file object” with read() and write() methods (and many more). To wrap a file descriptor in a “file object”, use fdopen().

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.