![](/img/trans.png)
[英]FileNotFoundError: [Errno 2] No such file or directory: 'python': 'python' while running a python script
[英]Error Running Python Script from Batch File FileNotFoundError: [Errno 2] No such file or directory: '[]'
当我尝试为python脚本运行批处理文件时,出现错误
D:\Sandbox\Python\Python Scripts>AQACompareBooks.py "D:/Sandbox/AQA/OrderAnalysis/Customer 1 OrderAnalysis -1 2018-05-08.xlsx"
[]
Traceback (most recent call last):
File "D:\Sandbox\Python\Python Scripts\AQACompareBooks.py", line 33, in <module>
xls = pd.ExcelFile(file)
File "D:\Dev\Sandbox\Python\lib\site-packages\pandas\io\excel.py", line 260, in __init__
self.book = xlrd.open_workbook(io)
File "D:\Dev\Sandbox\Python\lib\site-packages\xlrd\__init__.py", line 116, in open_workbook
with open(filename, "rb") as f:
FileNotFoundError: [Errno 2] No such file or directory: '[]'
批处理文件
AQACompareBooks.py "D:\Sandbox\AQA\OrderAnalysis\Customer1 OrderAnalysis -1 2018-05-08.xlsx"
Python代码
import pandas as pd
import numpy as np
import os
import sys
print(sys.argv[1:])
file = format(sys.argv[1:])
xls = pd.ExcelFile(file)
如果我在代码中手动设置路径,则一切正常。
即
file = 'D:\Sandbox\AQA\OrderAnalysis\Customer1 OrderAnalysis -1 2018-05-08.xlsx'
有人遇到过这个问题吗?
批处理文件和脚本位于同一文件夹中。 我要加载的文件不是
看起来很像是Windows问题,扩展名为.py
文件。 在一个Windows安装中,我检查了测试Python脚本在哪里接收参数,注册表项HKEY_CLASSES_ROOT\\Python.File\\Shell\\open\\command
设置为"C:\\WINDOWS\\py.exe" "%L" %*
(包括这些报价)。 尾随的%*
传递了Python然后存储在sys.argv
的参数。
其次是带有format(sys.argv[1:])
; 该片将始终产生一个列表,而所需的文件名存储在单个元素sys.argv[1]
。 只需使用它即可。
感谢tdelaney指出了它们,可以使用assoc
和ftype
命令检查关联(如果是管理员则设置关联):
C:\Users\yann>assoc .py
.py=Python.File
C:\Users\yann>ftype Python.File
Python.File="C:\WINDOWS\py.exe" "%L" %*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.