Unexpected character after continuation character - python

I am new to working with Python, and seem to be having an issue that no matter what I search, and no matter what I try, still presists.
I am trying to execute a system command using the exec function.
I have tried the following:
exec("/usr/sbin/something --arg")
Which returns invalid syntax with a cursor on the first /
I have tried
exec("\\usr\\sbin\\something --arg")
Which returns unexpected character after line continuation character and a cursor on the last character of my argument g in this case.
I have been searching for a solution for the last hour, and nothing has helped. How can I simply execute this command without an error being thrown?
Yes, my line endings are correct.

exec is used to execute Python code that is represented as a string or code object. From the docs:
exec(object[, globals[, locals]])
This function supports dynamic execution of Python code. object must be either a string or a code object. If it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error occurs).
To execute a system command, you can use os.system:
import os
os.system("/usr/sbin/something --arg")

Maybe try putting an r in front of the string so it reads exec(r"\\usr\\sbin\\something --arg")

Related

Python Converts the backslash followed by numbers into Unicode

I have a string which is a Windows path returned by another function. The function returns the path with a single backslash within it. Here I can't using raw string conversion to a variable. re.escape(path) does not work either. path.replace('\','\\') throws SyntaxError: unexpected character after line continuation character
The function returns a path something like "D:\Data\201909\Foo\20190927c\Files" which gets coverted into "D:\\Data\ü909\\Foo\x8190927c\\Files"
path can be assumed as the variable containing the value returned by the function.
Could you please suggest me a solution for this.
Thanks Much !
The below solution worked for me.
path = r"{}" .format(path)
where new variable path is the converted raw string.

GhPython vs Python: TypeErrorException from GH but not from Shell

I am using a library called py_openshowvar to communicate with a Kuka robot arm from grasshopper.
Everything works fine if I run the program from a command line python shell. However, when I run the same thing from GhPython, I get the following:
Not sure why I get the exceptions with GhPython but not when I run it outside the GH environment. The program still connects to the server and retrieves/sends the info I need, but I want to make sure and address this exception.
Thanks!
It is hard to tell you how to fix the error as you do not provide the code that triggers it, but in substance it comes from the fact that GHPython is IronPython (an implementation of Python based on .Net) whereas Python Shell is an implementation written in C.
The two implementations are similar but you sometimes hit a difference.
In your case the script is expecting a string or tuple but gets an IronPython.Runtime.Bytes.
Hmm, got bytes when expecting str looks like a unicode string vs byte string problem. You do no describe what are the versions of your CPython and GHPython, but you should know that Python 2 strings are byte strings while Python 3 ones are unicode strings.
If in Python 2 you can force a litteral string to be unicode by prepending it with u: u"foo" is a unicode string. You can also decode a byte string to its unicode version: b'ae\xe9\xe8'.decode('Latin1') is the unicode string u'aeéè'

Why does writing to stdout in console append the number of characters written, in Python 3?

I was just playing around with sys.stdout.write() in a Python console when I noticed that this gives some strange output.
For every write() call the number of characters written, passed to the function respectively gets append to the output in console.
>>> sys.stdout.write('foo bar')
for example results in
foo bar7 being printed out.
Even passing an empty string results in an output of 0.
This really only happens in a Python console, but not when executing a file with the same statements. More interestingly it only happens for Python 3, but not for Python 2.
Although this isn't really an issue for me as it only occurs in a console, I really wonder why it behaves like this.
My Python version is 3.5.1 under Ubuntu 15.10.
Apart from writing out the given string, write will also return the number of characters (actually, bytes, try sys.stdout.write('へllö')) As the python console prints the return value of each expression to stdout, the return value is appended to the actual printed value.
Because write doesn't append any newlines, it looks like the same string.
You can verify this with a script containing this:
#!/usr/bin/python
import sys
ret = sys.stdout.write("Greetings, human!\n")
print("return value: <{}>".format(ret))
This script should when executed output:
Greetings, human!
return value: <18>
This behaviour is mentioned in the docs here.

Python: Command line arguments not read?

I'm trying to read command line arguments in python in the form:
python myprogram.py string string string
I have tried using sys.argv[1-3] to get each string, but when I have a string such as $unny-Day, it does not process the entire string. How can I process strings like these entirely?
Are you using a shell? $ is a special character in the shell that is interpreted as a shell variable. Since the variable does not exist, it is textually substituted with an empty string.
Try using single quotes around your parameter, like > python myapp.py '$unny-Day'.

The meaning of the following bash script

I have a quick question. I got the following bash script from my friend, but I don't know what /inline/b64/ is, and how the following code segment works.
I have some experience with bash, and Python, but I cannot understand the following code fragment at all. Could anyone please give me some enlightenment?
More specifically,
1) What does /inline/b64 mean? I did some search on the web, but I couldn't find any clues.
2) What does the following command mean?
ENCODED_COMMAND=$(python <<EOF
3) What's the purpose of these kinds of encoding?
#!/bin/bash
COMMAND="FILTER file utterance_id /tmp/my_utt_list"
ENCODED_COMMAND=$(python <<EOF
import base64
print base64.urlsafe_b64encode('$COMMAND')
EOF
)
$BIN --edit_commands="/inline/b64/$ENCODED_COMMAND"
This depends on what the value of $BIN is. Presumably this is some other script which supports an --edit_commands flag. You would need to what that other script is expecting for this value to be able to interpret it.
This is combining a couple of bits of bash syntax. First, $(...) means "execute the enclosed command and capture its output as a string". Second, the <<EOF means that the following lines until the second EOF should be passed to the standard input of the command. So taken together, this is executing the Python script between the two EOFs, capturing its output, and assigning it to the ENCODED_COMMAND variable.
The script is taking some string, $COMMAND, and using the Python base64.urlsafe_b64encode function to encode it with Base64. The encoded string is then being passed to some unknown command, $BIN, which will presumably do something with it — perhaps decode and execute it in some way.

Categories