generating petsc binary file using python

611 views Asked by At

I am trying to create a PETSC binary file using python. I try to run the script on bash shell but I get an error

$ python -c 'print file.shape\n import sys,os\n sys.path.append(os.path.join(os.environ['PETSC_DIR'],'bin','pythonscripts'))\nimport PetscBinaryIO\nio=PetscBinaryIO.PetscBinaryIO()\nfile_fortran=file.transpose((2,1,0))\n io.writeBinaryFile('my_geometry.dat',(walls_fortran.rave1()).view(PetscBinaryIO.Vec),))'

Unexpected character after line continuation character.

I understand that happens because of an extra \ but my code doesn't seem to have any. I tried running python interactively to find which part of the code is faulty using python -i

>>> 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'

i am new to python so i know this might be an obvious error. but i don't seem to know what this is about

Thanks John for the answer. Now that it recognizes PETSC_DIR i get the error

 >>> 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>

It does not recognize PETSC_DIR even though i specify it

1

There are 1 answers

1
John1024 On BEST ANSWER

For illustration, let's take a small excerpt of that bash script:

python -c 'print file.shape\n import sys,os\n'

In a bash single-quoted string, as you have here, the characters "\n" represent a backslash followed by "n". Python sees this as an "extra backslash" even though you meant "\n" to be interpreted as a newline character. This is what generates errors of the type

unexpected character after line continuation character

To fix this, try instead:

python -c $'print file.shape\n import sys,os\n'

Bash treats $'...' strings specially and will, among other things, replace \n sequences with new line characters which python will understand and know how to process.

(The above will still give an error because file does not have a shape attribute. More on this below.)

There are other issues. Take, for example, this excerpt:

python -c 'sys.path.append(os.path.join(os.environ['PETSC_DIR'],'bin','pythonscripts'))'

After bash does quote removal, python sees:

sys.path.append(os.path.join(os.environ[PETSC_DIR],bin,pythonscripts))

This won't work because python needs PETSC_DIR and bin and pythonscripts to be quoted (with either single or double-quotes: python doesn't care). Try instead:

python -c 'sys.path.append(os.path.join(os.environ["PETSC_DIR"],"bin","pythonscripts"))'

When bash sees double-quotes inside single quotes, it leaves them alone. Thus, python will receive quoted strings where it needs them.

In sum, it looks to me like the error is caused not by your python code but by what bash does to your python code before passing it to python.

ADDENDUM: As for the print walls.shape() error where walls is a file handle, the error means what it says: file handles do not have shape attributes. Possibly you want to use the os.path.getsize(path) function from the os.path module to get file size in bytes?