如何在 xonsh 子进程中扩展 shell 变量?
How to expand shell variable in xonsh subprocess?
我正在学习 xonsh。我尝试了以下简单脚本,但它在 zipinfo -1 $mzip_str
失败,特别是在扩展 mzip_str
.
#!/usr/bin/env xonsh
from pathlib import Path
my_path = Path('/path/to/downloads/')
dir_list = my_path.glob('*.zip')
for my_zip in dir_list:
mzip_str = str(my_zip)
zip_dir_names = $(zipinfo -1 $mzip_str | grep -E '.*/$')
print(zip_dir_names)
在 xonsh shell 中,我得到了这些结果:
➤ zip_dir_names = $(zipinfo -1 mzip_str)
zipinfo: cannot find or open mzip_str, mzip_str.zip or mzip_str.ZIP.
➤ zip_dir_names = $(zipinfo -1 $mzip_str)
zipinfo: cannot find or open $mzip_str, $mzip_str.zip or $mzip_str.ZIP.
➤ zip_dir_names = $(zipinfo -1 @mzip_str)
zipinfo: cannot find or open @mzip_str, @mzip_str.zip or @mzip_str.ZIP.
➤ zip_dir_names = $(zipinfo -1 !mzip_str)
zipinfo: cannot find or open mzip_str, mzip_str.zip or mzip_str.ZIP.
The @(<expr>)
operator form works in subprocess mode, and will evaluate arbitrary Python code. The result is appended to the subprocess command list. If the result is a string or bytes, it is appended to the argument list. If the result is a list or other non-string sequence, the contents are converted to strings and appended to the argument list in order. If the result in the first position is a function, it is treated as an alias (see the section on Aliases below), even if it was not explicitly added to the aliases
mapping. Otherwise, the result is automatically converted to a string.
所以相关的行应该看起来像(未经测试,因为我没有安装这个 xonsh
):
zip_dir_names = $(zipinfo -1 @(my_zip) | grep -E '.*/$')
但它听起来像 $()
returns 一个字符串,如果除了打印它之外您想对它做任何事情,您可能希望 zip_dir_names
成为一个列表。像
zip_dir_names = [ name for name in !(zipinfo -1 @(my_zip)) if name.endsWith("/") ]
可能效果更好。当然,或者只使用 standard Python zipfile module 模块而不是外部程序。
zip_dir_names = [ f.filename for f in ZipFile(my_zip).infolist() if f.is_dir() ]
我正在学习 xonsh。我尝试了以下简单脚本,但它在 zipinfo -1 $mzip_str
失败,特别是在扩展 mzip_str
.
#!/usr/bin/env xonsh
from pathlib import Path
my_path = Path('/path/to/downloads/')
dir_list = my_path.glob('*.zip')
for my_zip in dir_list:
mzip_str = str(my_zip)
zip_dir_names = $(zipinfo -1 $mzip_str | grep -E '.*/$')
print(zip_dir_names)
在 xonsh shell 中,我得到了这些结果:
➤ zip_dir_names = $(zipinfo -1 mzip_str)
zipinfo: cannot find or open mzip_str, mzip_str.zip or mzip_str.ZIP.
➤ zip_dir_names = $(zipinfo -1 $mzip_str)
zipinfo: cannot find or open $mzip_str, $mzip_str.zip or $mzip_str.ZIP.
➤ zip_dir_names = $(zipinfo -1 @mzip_str)
zipinfo: cannot find or open @mzip_str, @mzip_str.zip or @mzip_str.ZIP.
➤ zip_dir_names = $(zipinfo -1 !mzip_str)
zipinfo: cannot find or open mzip_str, mzip_str.zip or mzip_str.ZIP.
The
@(<expr>)
operator form works in subprocess mode, and will evaluate arbitrary Python code. The result is appended to the subprocess command list. If the result is a string or bytes, it is appended to the argument list. If the result is a list or other non-string sequence, the contents are converted to strings and appended to the argument list in order. If the result in the first position is a function, it is treated as an alias (see the section on Aliases below), even if it was not explicitly added to thealiases
mapping. Otherwise, the result is automatically converted to a string.
所以相关的行应该看起来像(未经测试,因为我没有安装这个 xonsh
):
zip_dir_names = $(zipinfo -1 @(my_zip) | grep -E '.*/$')
但它听起来像 $()
returns 一个字符串,如果除了打印它之外您想对它做任何事情,您可能希望 zip_dir_names
成为一个列表。像
zip_dir_names = [ name for name in !(zipinfo -1 @(my_zip)) if name.endsWith("/") ]
可能效果更好。当然,或者只使用 standard Python zipfile module 模块而不是外部程序。
zip_dir_names = [ f.filename for f in ZipFile(my_zip).infolist() if f.is_dir() ]