jpcgandre
jpcgandre

Reputation: 1505

Error when opening txt file in Python

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

Upvotes: 0

Views: 627

Answers (1)

gertvdijk
gertvdijk

Reputation: 24844

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().

Upvotes: 3

Related Questions