I’m on a Ubuntu 20.04 system, and I’m using Python 3.8 to write a script that does multiple things using configurable lines of bash, but one of them is that it creates desktop shortcuts.
This single-line command creates a desktop shortcut, and works flawlessly when I execute it directly in my terminal:
echo "[Desktop Entry]"$'n'"Type=Application"$'n'"Name[en_US]=Documents"$'n'"Exec=pcmanfm ~/Documents"$'n'"Icon=system-file-manager" > ~/Desktop/1;
However, when I execute it in Python, like so:
foobar.py
rl = """echo "[Desktop Entry]"$'n'"Type=Application"$'n'"Name[en_US]=Documents"$'n'"Exec=pcmanfm ~/Documents"$'n'"Icon=system-file-manager" > ~/Desktop/1;"""
subprocess.run(rl, shell=True)
…instead of creating a desktop shortcut with the proper name, icon, and action, it creates an empty file that contains the following text:
0.txt
:
[Desktop Entry]$nType=Application$nName[en_US]=Sign Out$nExec=/home/agent/QaSC/login/login.bin$nIcon=system-switch-user
Is there any particular reason why Python would be handling the newline characters differently than the bash shell does, and if so, how can I resolve this problem?
2
Answers
$'...'
is abash
extension, but the default shell used whenshell=True
is specified issh
. Use theexecutable
option to specify an alternate shell.Since the argument to
echo
has quotes, it could contain literal newlines at the command line, and therefore also in the Python process. I see no reason to use the Bash extension$'n'
syntax.(It’s also unclear why the Python code doesn’t just write a file in the normal way, but I assume the command in the OP is essentially a placeholder.)