运行 来自命令行参数的 .py 代码,使用 Enthought Canopy

Running a .py code from Command Line Argument, using Enthought Canopy

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

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'

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

也就是说,
sys.argv[0] 分配为 MergeFilesv1_2.py
sys.argv[1] 被分配为 "C:\Desktop\MasterFile.xlsx" 等等。