我正在编写一个 Python 脚本,该脚本在我的 Linux NAS 上执行并进入了有趣的情况。
访问以感叹号开头的文件/目录时出现问题(历史扩展)。
import os
path = "/volume1/Images/!Test/DSC_5062.NEF"
os.stat(path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '/volume1/Images/!Test/DSC_5062.NEF'
如果我尝试转义感叹号,我会得到以下结果:
import os
path = "/volume1/Images/\!Test/DSC_5062.NEF"
os.stat(path)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 2] No such file or directory: '/volume1/Images/\\!Test/DSC_5062.NEF'
知道该怎么做吗?顺便说一句,第一个代码片段在我的 MacOS 机器上运行没有任何问题,但在我的 Linux NAS 上不起作用。
为了提供完整的信息,如果我在命令行上执行以下操作,我将得到以下结果。
admin@nas:/tmp$ ls /volume1/Images/!Test/DSC_5062.NEF
-sh: !Test/DSC_5062.NEF: event not found
admin@nas:/tmp$ ls /volume1/Images/\!Test/DSC_5062.NEF
/volume1/Images/!Test/DSC_5062.NEF
感谢您的帮助。
米哈尔
最佳答案
尝试将其放在引号中:
import os
path = "/volume1/Images/"'!'"Test/DSC_5062.NEF"
os.stat(path)
关于Python 和历史扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42320248/