使用python生成petsc二进制文件

使用python生成petsc二进制文件,python,linux,bash,petsc,Python,Linux,Bash,Petsc,我正在尝试使用python创建一个PETSC二进制文件。我试图在bashshell上运行脚本,但出现了一个错误 Unexpected character after line continuation character. $python-c'print file.shape\n import sys,os\n sys.path.append(os.path.join(os.environ['PETSC_DIR','bin','pythonscripts'))\nimport-petscbin

我正在尝试使用python创建一个PETSC二进制文件。我试图在bashshell上运行脚本,但出现了一个错误

Unexpected character after line continuation character.
$python-c'print file.shape\n import sys,os\n sys.path.append(os.path.join(os.environ['PETSC_DIR','bin','pythonscripts'))\nimport-petscbinaryo\nio=petscbinaryo.petscbinaryo()\nfile\u-fortran=file.transposse((2,1,0))\n io.writeBinaryFile('my_-geometry.dat',(walls\u-fortran.rave1()).view(petscryo.Vec)).Vec))“

Unexpected character after line continuation character.
我知道发生这种情况是因为有一个额外的
\
,但我的代码似乎没有。我尝试以交互方式运行python,以使用python-I查找代码的哪一部分出错

Unexpected character after line continuation character.
>>> walls=open('file.dat','r+')
>>> print walls.shape()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'file' object has no attribute 'shape'
>walls=open('file.dat','r+'))
>>>打印墙。shape()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
AttributeError:“文件”对象没有属性“形状”
我是python新手,所以我知道这可能是一个明显的错误。但我似乎不知道这是怎么回事 谢谢约翰的回答。 既然它识别了PETSC_DIR,我就得到了错误

Unexpected character after line continuation character.
 >>> PETSC_DIR="/home/petsc-3.4.3"
 >>> sys.path.append(os.path.join(os.environ["PETSC_DIR"],"bin","pythonscripts"))

     Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "/usr/lib64/python2.6/UserDict.py", line 22, in __getitem__
     raise KeyError(key)
     KeyError: 'PETSC_DIR'</code>
>>PETSC_DIR=“/home/PETSC-3.4.3”
>>>sys.path.append(os.path.join(os.environ[“PETSC_DIR”],“bin”,“pythonscripts”))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
文件“/usr/lib64/python2.6/UserDict.py”,第22行,在__
升起钥匙错误(钥匙)
KeyError:'PETSC_DIR'

它不承认PETSCYDIR,即使我指定了

作为例证,让我们来对BASH脚本的一个小摘要:

Unexpected character after line continuation character.
python -c 'print file.shape\n import sys,os\n'
在bash单引号字符串中,正如您在这里看到的,字符“\n”表示后跟“n”的反斜杠。Python认为这是一个“额外的反斜杠”,即使您的意思是将“\n”解释为换行符。这就是产生这种类型错误的原因

Unexpected character after line continuation character.
unexpected character after line continuation character
要解决此问题,请尝试:

Unexpected character after line continuation character.
python -c $'print file.shape\n import sys,os\n'
Bash专门处理
$'…'
字符串,除其他外,它将用python理解并知道如何处理的新行字符替换
\n
序列

Unexpected character after line continuation character.
(由于
文件
没有
形状
属性,上面仍然会给出一个错误。下面将对此进行详细说明。)

Unexpected character after line continuation character.
还有其他问题。以这段摘录为例:

Unexpected character after line continuation character.
python -c 'sys.path.append(os.path.join(os.environ['PETSC_DIR'],'bin','pythonscripts'))'
bash删除引号后,python会看到:

Unexpected character after line continuation character.
sys.path.append(os.path.join(os.environ[PETSC_DIR],bin,pythonscripts))
这不起作用,因为python需要引用
PETSC_DIR
bin
pythonscripts
(单引号或双引号:python不在乎)。请尝试:

Unexpected character after line continuation character.
python -c $'print file.shape\n import sys,os\n'
python-c'sys.path.append(os.path.join(os.environ[“PETSC_DIR”],“bin”,“pythonscripts”))

Unexpected character after line continuation character.
当bash在单引号中看到双引号时,它会将它们单独留下。因此,python将在需要时接收带引号的字符串

Unexpected character after line continuation character.
总之,在我看来,错误不是由python代码引起的,而是由bash在将python代码传递给python之前对python代码所做的操作引起的

Unexpected character after line continuation character.

附录:对于
print walls.shape()
error,其中
walls
是一个文件句柄,错误的意思是:文件句柄没有
shape
属性。您可能希望使用
os.path
模块中的函数来获取文件大小(以字节为单位)

dbl chk,确保在该延续字符之后没有空格或制表符。祝你好运。谢谢@John1024!那有帮助!!是的,问题是在bash中运行python!
Unexpected character after line continuation character.