简体   繁体   中英

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

I have prepared a Python script which reads from Excel files:

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)

If I execute it through Jupyter Notebook, it executes successfully.

But when I run it from the command line by passing the below parameters it gives me an error

Command Line:

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

Error Message:

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'

Your file's name is also an argument.

Which means,
sys.argv[0] is assigned as MergeFilesv1_2.py ,
sys.argv[1] is assigned as "C:\\Desktop\\MasterFile.xlsx" and so on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM