在Python中打开txt文件时出错

2024-04-20 10:56:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我必须使用txt文件,为此我使用了以下代码:

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

但是,当我在一个特定的应用程序中运行这一行以运行另一个程序中可用的python脚本时,我得到了这个错误。我在Spyder中运行时没有出错。在

^{pr2}$

我不知道为什么会发生这个错误。。。。在

编辑: 代码行直到有问题的行

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(',')])

错误是:

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

Tags: pathnameinfromimportjobsjobsteps
1条回答
网友
1楼 · 发布于 2024-04-20 10:56:27

from os import *

您正在导入全局命名空间中的所有os内容,包括^{}。别这样。在

第二个参数,flags在提供单个字符串r时被定义为整数常量。这基本上是什么DSM was telling you和什么Lattyware said。在

默认情况下,Python中包含在全局命名空间中的open()显然是不同的:

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

相关问题 更多 >