用echo用echo file file in shell中的newline字符,但在python 3中。

发布于 2025-02-05 11:50:12 字数 879 浏览 3 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柠檬心 2025-02-12 11:50:12

$'...'bash扩展名,但是指定shell = true指定时使用的默认外壳是sh 。使用可执行文件选项来指定备用外壳。

subprocess.run(rl, shell=True, executable='/bin/bash')

$'...' is a bash extension, but the default shell used when shell=True is specified is sh. Use the executable option to specify an alternate shell.

subprocess.run(rl, shell=True, executable='/bin/bash')
厌味 2025-02-12 11:50:12

由于echo的参数具有引号,因此它可以在命令行中包含字面的新线,因此也可以在Python过程中。我认为没有理由使用Bash Extension $'\ n'语法。

$ echo "foo
> bar"
foo
bar
$ python
Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.run('echo "foo\nbar"', shell=True)
foo
bar
CompletedProcess(args='echo "foo\nbar"', returncode=0)
>>> 

(还不清楚为什么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.

$ echo "foo
> bar"
foo
bar
$ python
Python 3.8.10 (default, Mar 15 2022, 12:22:08) 
[GCC 9.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import subprocess
>>> subprocess.run('echo "foo\nbar"', shell=True)
foo
bar
CompletedProcess(args='echo "foo\nbar"', returncode=0)
>>> 

(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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文