Question:
Traceback error in python?
LINCY
2013-06-19 08:11:23 UTC
I am getting the following traceback error:
Traceback (most recent call last):
** IDLE Internal Exception:
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/idlelib/run.py", line 334, in runcode
exec(code, self.locals)
File "/Volumes/local/io-works/home/shared/sync/run.py", line 6, in
name = sys.argv[1]
IndexError: list index out of range

The entire python code:

import os
import sys
import fileinput
import datetime
from time import localtime
name = sys.argv[1]
# log specifies the file that is supposed to be parsed by the script
log = fileinput.input('/var/log/secure.log')
L = localtime()
CalendarMonth = L[1]
today = L[2]
Month = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
MonthVar = CalendarMonth - 1
MonthName = Month[MonthVar]
#
#def timetest determines whether the user logged in within the last hour
#
def timetest(hour,min, sec, Hour, Min, Sec):
x = (int(hour)*60)+int(min)+(int(sec)/60) - 180
y = (int(Hour)*60)+int(Min)+(int(Sec)/60)
if y >= x:
z = 'true'
else:
z = 'false'
return z
login = 'User info context values set for'
logwindow = 'Showing Login Window'
failedauth = 'Failed to authenticate'
firstlist = []
for line in log:
if MonthName in line:
h = line.split()
day = float(h[1])
if day == today:
if login in line:
firstlist.append(line)
elif failedauth in line:
firstlist.append(line)
elif logwindow in line:
firstlist.append(line)
else: pass
else: pass
else: pass
firstlistlength = int(len(firstlist))
if firstlistlength == 0:
pass
else:
count1 = 0
count2 = 0
actuallist = []
while count1 <= count2:
if count1 == count2:
tryline = firstlist[count1]
count3 = count1 + 1
try:
nextline = firstlist[count3]
except IndexError:
actuallist.append(tryline)
break
if login in tryline:
if failedauth in nextline:
count1 = count1 + 1
count2 = count2 + 1
else:
actuallist.append(tryline)
count1 = count1 + 1
count2 = count2 + 1
else:
count1 = count1 + 1
count2 = count2 + 1
else:
pass
if firstlistlength == 0:
pass
else:
actuallist.reverse()
lastlogin = []
namelist = []
for line in actuallist:
if name in namelist:
pass
elif name in line:
lastlogin.append(line)
namelist.append(name)
else:
lastlogin.append(line)
intlastlogin = int(len(lastlogin))
if int(len(namelist)) == 0:
pass
else:
if intlastlogin == 1:
#this is were the actual rsync command is executed
rsync = 'rsync -rlptoDuz --ignore-errors --bwlimit=3000 --exclude-from=/home/shared/sync/exclude --delete /home/'+name+'/ /Volumes/local/io-works/home/'+name+'/'
os.system(rsync)
else:
intnextline = int(intlastlogin - 2)
nextline = lastlogin[intnextline]
sys.stdout.write(nextline)
sys.stdout.write('\n')
tim = datetime.datetime.now()
hour = '%d' %tim.hour
min = '%d' %tim.minute
sec = '%d' %tim.second
index1 = nextline.split()
Time = index1[2]
Hour = Time[:2]
Min = Time[3:5]
Sec = Time[6:]
test = timetest(hour,min, sec, Hour, Min, Sec)
if test == 'true':
#this is were the actual rsync command is executed
rsync = 'rsync -rlptoDuz --ignore-errors --bwlimit=3000 --exclude-from=/home/shared/sync/exclude --delete /home/'+name+'/ /Volumes/local/io-works/home/'+name+'/'
os.system(rsync)
else:
pass


The code works absolutely perfectly with MAC OS 10.5.8 but it is giving me error in MAC OS 10.8.2.

Please see the code in case you could figure out the problem
Four answers:
Zarn
2013-06-19 12:33:33 UTC
There's no indentation in your copypasta. That's a problem, seeing as how Python uses that indentation to define their code. I'm suggesting you just read the error message carefully - it says that a particular list index is out of bounds. Are you sure that particular list even has a [1] element?
lopez
2017-01-22 11:00:04 UTC
1
Russel
2013-06-20 07:39:57 UTC
The error you're getting (IndexError) happens when you reference a value from a list at some index that is greater than the size of the list.



Example:

>>> a = [5, 6, 7]

>>> a[2]

7

>>> a[3]



Traceback (most recent call last):

File "", line 1, in

a[3]

IndexError: list index out of range

>>>



sys.argv is a list that contains command line arguments passed to the script when the script is run.



The first item in sys.argv will be the path to the script itself (in otherwords the first item is the script itself). The rest after that are the command line arguments.



To be clear, this is the usual way you run your script from command line (terminal in mac).



python myscript.py



And to pass it command line arguments you do



python myscript.py somearg anotherarg



and if you print sys.argv from within that script you may see something like



['C:\\users\\me\\desktop\\myscript.py', 'somearg', 'anotherarg']



I don't know the root cause of your error. It could be the way you're running the script in the first place.

If you're running it from the terminal make sure your not forgetting to put pass it the argument in question. But I really don't know.
?
2016-11-08 10:33:19 UTC
It looks such as you tried to characteristic an integer (5) with the a console enter fee (f). Console enter is frequently of type 'string'. in case you opt to characteristic them you will would desire to hunt for the thank you to transform a string to an integer in python. ultimately, you will probably finally end up with some thing like: f.ConvertToInt() + 5 (the place the ConvertToInt is the approach that converts the string 'f' to an integer).


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...