使用Enthought Canopy从命令行参数运行.py代码

时间:2017-01-17 07:41:26

标签: python python-2.7 enthought canopy

我准备了一个从Excel文件中读取的Python脚本:

import glob
import pandas as pd
import StringIO
import sys
import os
from xlrd import open_workbook

filelocation = str(sys.argv[0])
outputlocation = str(sys.argv[1])
FileExtension = str(sys.argv[2])
Separator = str(sys.argv[3])



#filelocation = 'C:\Desktop\MasterFile.xlsx'
#outputlocation = 'C:\Desktop\output'
wb = open_workbook(filelocation)

如果我通过Jupyter Notebook执行它,它会成功执行。

但是当我通过传递以下参数从命令行运行它时,它会给我一个错误

命令行:

python MergeFilesv1_2.py "C:\Desktop\MasterFile.xlsx" "C:\Desktop\output" ".csv" ","

错误消息:

Traceback (most recent call last):
File "MergeFilesv1_2.py",line 22, in <module>
wb = open_workbook(filelocation)
File "C:\Users\Sha\appData\Local\Enthought\Canopy\User\lib\site-packages\xlrd\_init__.py",line 441, in open_workbook
ragged_rows=ragged_rows,
File "C:\Users\Sha\appData\Local\Enthought\Canopy\User\lib\site-packages\xlrd\book.py",line 91, in open_workbook_xls
biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
File "C:\Users\Sha\appData\Local\Enthought\Canopy\User\lib\site-packages\xlrd\book.py",line 1230, in getbof
bof_error('Expected BOF record; found %r' %SELF.MEM[SAVPOS:SAVPOS + 8])
File "C:\Users\Sha\appData\Local\Enthought\Canopy\User\lib\site-packages\xlrd\book.py",line 1224, in bof_error
raise XLRDError('Unsupported format, or corrupt file:' + msg)
xlrd.biffh.XLRDError:Unsupported format, or corrupt file: Expected BOF record, found '\n# codin'

1 个答案:

答案 0 :(得分:2)

您的文件名也是一个参数。

这意味着,
sys.argv[0]被指定为MergeFilesv1_2.py
sys.argv[1]被指定为"C:\Desktop\MasterFile.xlsx",依此类推。