用echo用echo file file in shell中的newline字符,但在python 3中。
我在 ubuntu 20.04 系统中它创建桌面快捷方式。
此单行命令创建一个桌面快捷方式,,当我直接在终端中执行它时,可以完美地工作:
echo "[Desktop Entry]"$'\n'"Type=Application"$'\n'"Name[en_US]=Documents"$'\n'"Exec=pcmanfm ~/Documents"$'\n'"Icon=system-file-manager" > ~/Desktop/1;
但是,当我在python中执行它时,就像:
foobar.py.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)
...而不是使用专有名称,图标和操作创建桌面快捷方式,而是创建一个空的文件,其中包含以下文本:
: 0.TXT
:
[Desktop Entry]$\nType=Application$\nName[en_US]=Sign Out$\nExec=/home/agent/QaSC/login/login.bin$\nIcon=system-switch-user
is Python与Bash Shell的处理方式不同的原因有什么特殊原因,如果是,我该如何解决此问题?
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]"
However, when I execute it in Python, like so:
foobar.py
rl = """echo "[Desktop Entry]"
...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?
\n'"Type=Application"
However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Name[en_US]=Documents"
However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Exec=pcmanfm ~/Documents"
However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Icon=system-file-manager" > ~/Desktop/1;
However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Type=Application"
...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
:
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?
\n'"Type=Application"
However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Name[en_US]=Documents"
However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Exec=pcmanfm ~/Documents"
However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Icon=system-file-manager" > ~/Desktop/1;
However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Name[en_US]=Documents"...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
:
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?
\n'"Type=Application"However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Name[en_US]=Documents"However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Exec=pcmanfm ~/Documents"However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Icon=system-file-manager" > ~/Desktop/1;However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Exec=pcmanfm ~/Documents"...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
:
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?
\n'"Type=Application"However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Name[en_US]=Documents"However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Exec=pcmanfm ~/Documents"However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Icon=system-file-manager" > ~/Desktop/1;However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\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
:
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?
\n'"Type=Application"However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Name[en_US]=Documents"However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Exec=pcmanfm ~/Documents"However, when I execute it in Python, like so:
foobar.py
...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
:
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?
\n'"Icon=system-file-manager" > ~/Desktop/1;However, when I execute it in Python, like so:
foobar.py
...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
:
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
$'...'
是bash
扩展名,但是指定shell = true
指定时使用的默认外壳是sh 。使用
可执行文件
选项来指定备用外壳。$'...'
is abash
extension, but the default shell used whenshell=True
is specified issh
. Use theexecutable
option to specify an alternate shell.由于
echo
的参数具有引号,因此它可以在命令行中包含字面的新线,因此也可以在Python过程中。我认为没有理由使用Bash Extension$'\ n'
语法。(还不清楚为什么Python代码不仅以正常方式编写文件,而且我认为OP中的命令本质上是占位符。)
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.)