【问题标题】:Error while filehandling in Blender using Python使用 Python 在 Blender 中处理文件时出错
【发布时间】:2011-06-24 06:57:22
【问题描述】:

我正在尝试从连接到Arduino 的外部设备读取Blender 中的数据并保存到文件中。但它给出了这个错误

SyntaxError: invalid syntax
Python script error from controller "contr#CONTR#1":
Traceback (most recent call last):
  File "SerialBGE.py", line 6, in <module>
    f=open('abc.dat', 'r')
IOError: [Errno 2] No such file or directory: 'abc.dat'

我的代码是正确的,但我不明白问题所在。

serial.py:

import GameLogic
import pickle
import os

os.system('dane.py')
f=open('abc.dat', 'r')
print "abc.dat = "
x=pickle.load(f)
print x
print "end of abc.dat"
f.close();
y=x[:]
for z in x:
    y.remove(z)
    print "removing " + str(z)
    print str(y) + " and " + str(x)
    f=open('abc.dat', 'w')
    pickle.dump(y, f)
    f.close()
    contr = GameLogic.getCurrentController()
    location=contr.actuators["loc"]
    y = 0.001*(ord(z)-128)
    location.dLoc=[y,0,0]
    contr.activate(location)

dane.py:

import serial
import pickle

with serial.Serial(COM3,9600) as port, open('abc.dat','r') as f:
    for i in range(0, 10):
        x = port.read(size=1)
        y=pickle.load(f)
        f.close()
        f=open('abc.dat','w')
        for i in x:
            y.append(i)
        pickle.dump(y,f)
        f.close()
port.close()

【问题讨论】:

    标签: python arduino blender


    【解决方案1】:

    您收到错误是因为第一次运行程序时文件“abc.dat”不存在。

    您需要处理初始情况,当您第一次运行程序时,在“abc.dat”存在之前。

    【讨论】:

    • 所以 m 读取、附加然后写入最终数据。在那种情况下,我的代码很好。当我放置自己的数据而不是 port.read() 时,它工作正常。
    • 这很好,但请考虑一下您的结构:您读取文件,添加新数据,然后将其写回。但是当你第一次运行它时会发生什么?该文件不存在可供读取 - 您需要处理这种情况。
    猜你喜欢
    • 2018-10-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-04
    • 1970-01-01
    • 2015-01-07
    • 2020-07-16
    • 2011-05-05
    相关资源
    最近更新 更多