Issue
I'm trying to learn python / pandas. I'm working through "Foundations for Analytics for Python' but have hit a wall
using
input_file = sys.argv[1]
gives result
File "C:\Users\longr\Desktop\pfile\1excel_introspect_workbook.py", line 11, in input_file = sys.argv[1] IndexError: list index out of range
In previous excercises replacing this call with
input_file = 'supplier_data.csv'
works... [for a csv file] I've used the source code from github - same error. All my files [.py / .xlsx / .csv] are together in C:\Users\longr\Desktop\pfile\ .... but I'm at a loss
Can anyone help please?
import sys
from xlrd import open_workbook
input_file = sys.argv[1]
workbook = open_workbook(input_file)
print('Number of worksheets:', workbook.nsheets)
for worksheet in workbook.sheets():
print("Worksheet name:", worksheet.name, "\tRows:", worksheet.nrows, "t\Columns:", worksheet.ncols)
Solution
sys.argv[1]
is the third input that you enter to run your script after the name of your python file.
let's say your py script is named example.py
then you will run it like
python example.py
but if you want to get the csv file as argv[1]
then you need to run your script like
python example.py supplier_data.csv
now your
argv[0] == example.py
and
argv[1] == supplier_data.csv
as string type.
Answered By - Ehsan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.