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?