使用 nautilus 脚本转义路径中的空格
我不认为这会像结果那样棘手,但我来了。我正在尝试用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为
$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
应该以换行符分隔,而不是空格分隔。在这种情况下,bash 应该可以执行以下操作:I think that
$NAUTILUS_SCRIPT_SELECTED_FILE_PATHS
should be newline delimited, not space delimited. In that case, the following should work from bash:跳过一个级别的评估表明 nautilus 文档是不完整的,并且有一种更好的方法,更少受到谁知道有多少级别的解释的影响。 Nautilus 将选定的文件作为脚本参数传递:
我将输出发送到
/tmp
因为我不想寻找标准输出。给定:选择目录中的所有文件,Scripts->arguments.sh 产生:
我可以引用
$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:
I sent the output to
/tmp
since I didn't want to hunt for the stdout. Given:Select all the files in the directory and Scripts->arguments.sh yields:
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, butargv
stays unmolested.Use argv (or
sys.argv
in Python) and save some headache. Also the documented semantics of the environment variables is weird.我无法测试这个,因为我在 Windows 机器上,但是你尝试过使用 $NAUTILUS_SCRIPT_SELECTED_FILE_URIS 代替吗?然后,在 python 中,你可以通过以下方式获取路径:
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:
事实证明,
echo
有时会将变量中的换行符转换为空格。以下应该有效:It turns out,
echo
sometimes turns newlines in variables into spaces. The following should work: