使用 nautilus 脚本转义路径中的空格

发布于 2024-09-04 04:35:35 字数 703 浏览 4 评论 0原文

我不认为这会像结果那样棘手,但我来了。我正在尝试用 Python 编写一个 Nautilus 脚本,只需选择并右键单击它们即可将一张或多张图像上传到 Imgur。它适用于单个图像和多个图像 - 只要它们不包含任何空格。事实上,您可以上传包含空白的单个图像,但不能上传多个图像。

问题在于 NAUTILUS_SCRIPT_SELECTED_FILE_PATHS 以空格分隔的字符串形式返回所有选定的文件和目录。例如,它可能看起来像这样:

print os.environment['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS']
/home/nevon/Desktop/test image.png /home/nevon/Desktop/test.jpg

我需要的是一种方法 - 在 bash 或 Python 中 - 转义路径中的空格 - 但不是分隔不同项目的空格。或者是这样,或者是在每个项目周围加上引号的方法。

最终的解决方案是,如果我可以在 bash 中执行此操作,然后将这些项目作为单独的参数发送到我的 python 脚本。比如:

python uploader.py /home/nevon/Desktop/test\ image.png /home/nevon/Desktop/test.jpg

我尝试过 RTFM'ing,但似乎没有很多好的解决方案。至少我没有发现。有什么想法吗?

I didn't think this would be as tricky as it turned out to be, but here I am. I'm trying to write a Nautilus script in Python to upload one or more images to Imgur just by selecting and right clicking them. It works well enough with both single images and multiple images - as long as they don't contain any whitespace. In fact, you can upload a single image containing whitespace, just not multiple ones.

The problem is that NAUTILUS_SCRIPT_SELECTED_FILE_PATHS returns all the selected files and directories as a space separated string. So for example, it could look like this:

print os.environment['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS']
/home/nevon/Desktop/test image.png /home/nevon/Desktop/test.jpg

What I need is a way to -either in bash or Python- escape the spaces in the path - but not the spaces that delimit different items. Either that, or a way to put quotation marks around each item.

The ultimate solution would be if I could do that in bash and then send the items as separate arguments to my python script. Something like:

python uploader.py /home/nevon/Desktop/test\ image.png /home/nevon/Desktop/test.jpg

I've tried RTFM'ing, but there doesn't seem to be a lot of good solutions for this. At least not that I've found. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

请叫√我孤独 2024-09-11 04:35:35

我认为 $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS 应该以换行符分隔,而不是空格分隔。在这种情况下,bash 应该可以执行以下操作:

echo -e $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | xargs python uploader.py 

I think that $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS should be newline delimited, not space delimited. In that case, the following should work from bash:

echo -e $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS | xargs python uploader.py 
痴情换悲伤 2024-09-11 04:35:35

跳过一个级别的评估表明 nautilus 文档是不完整的,并且有一种更好的方法,更少受到谁知道有多少级别的解释的影响。 Nautilus 将选定的文件作为脚本参数传递:

$ cat ~/.gnome2/nautilus-scripts/arguments.sh
#!/bin/sh
rm -f /tmp/arguments.*
outf=/tmp/arguments.$
echo "$0: $#" > $outf
while [ $# -gt 0 ] ; do
    echo "$1"
     if [ ! -r $1 ] ; then echo "cwd is not correct"; fi
    shift
done >> $outf
echo paths $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS >> $outf
for i in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS ; do
    echo "+$i+"
done >> $outf

我将输出发送到 /tmp 因为我不想寻找标准输出。给定:

$ ls -1
a
b
c with space
d
e with space
g

选择目录中的所有文件,Scripts->arguments.sh 产生:

$ cat /tmp/arguments.20447 
/home/msw/.gnome2/nautilus-scripts/arguments.sh: 6
a
b
c with space
d
e with space
g
paths /home/msw/junk/a /home/msw/junk/b /home/msw/junk/c with space 
  /home/msw/junk/d /home/msw/junk/e with space 
  /home/msw/junk/g
+/home/msw/junk/a+
+/home/msw/junk/b+
+/home/msw/junk/c+
+with+
+space+
+/home/msw/junk/d+
+/home/msw/junk/e+
+with+
+space+
+/home/msw/junk/g+

我可以引用 $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS 来避免这种情况吗?当然。我没有证明变量的插值有多少级是有问题的,但 argv 保持不受干扰。

使用 argv(或 Python 中的 sys.argv)可以避免一些麻烦。另外,环境变量的记录语义很奇怪。

Skipping one level of evaluation shows that the nautilus documentation is incomplete and that there is a better way much less subject to who-knows-how-many levels of interpretation. Nautilus passes selected files as script arguments:

$ cat ~/.gnome2/nautilus-scripts/arguments.sh
#!/bin/sh
rm -f /tmp/arguments.*
outf=/tmp/arguments.$
echo "$0: $#" > $outf
while [ $# -gt 0 ] ; do
    echo "$1"
     if [ ! -r $1 ] ; then echo "cwd is not correct"; fi
    shift
done >> $outf
echo paths $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS >> $outf
for i in $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS ; do
    echo "+$i+"
done >> $outf

I sent the output to /tmp since I didn't want to hunt for the stdout. Given:

$ ls -1
a
b
c with space
d
e with space
g

Select all the files in the directory and Scripts->arguments.sh yields:

$ cat /tmp/arguments.20447 
/home/msw/.gnome2/nautilus-scripts/arguments.sh: 6
a
b
c with space
d
e with space
g
paths /home/msw/junk/a /home/msw/junk/b /home/msw/junk/c with space 
  /home/msw/junk/d /home/msw/junk/e with space 
  /home/msw/junk/g
+/home/msw/junk/a+
+/home/msw/junk/b+
+/home/msw/junk/c+
+with+
+space+
+/home/msw/junk/d+
+/home/msw/junk/e+
+with+
+space+
+/home/msw/junk/g+

Could I have quoted $NAUTILUS_SCRIPT_SELECTED_FILE_PATHS to avoid this? Sure. I didn't to demonstrate that how many levels of interpolation with the variable is questionable, but argv stays unmolested.

Use argv (or sys.argv in Python) and save some headache. Also the documented semantics of the environment variables is weird.

南街女流氓 2024-09-11 04:35:35

我无法测试这个,因为我在 Windows 机器上,但是你尝试过使用 $NAUTILUS_SCRIPT_SELECTED_FILE_URIS 代替吗?然后,在 python 中,你可以通过以下方式获取路径:

[f.strip() for f in os.environment['NAUTILUS_SCRIPT_SELECTED_FILE_URIS'].split('file://') if len(f) > 0]

I can't test this as I'm at a Windows machine, but have you tried using $NAUTILUS_SCRIPT_SELECTED_FILE_URIS instead? Then, in python you could get the paths with something like:

[f.strip() for f in os.environment['NAUTILUS_SCRIPT_SELECTED_FILE_URIS'].split('file://') if len(f) > 0]
撩起发的微风 2024-09-11 04:35:35

事实证明,echo 有时会将变量中的换行符转换为空格。以下应该有效:

echo -e "$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS" | xargs python uploader.py

It turns out, echo sometimes turns newlines in variables into spaces. The following should work:

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