473,399 Members | 2,146 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

BurnTard has got a new IndexError

Okay, so I know what an IndexError is and all, but that doesn't help me understand why on earth I'm getting this one. I'm writing a program that's supposed skim through directories, dump all files into one list and all directories into another. That's as far as I've gotten now because this is where the nasty error shows up at the very beginning. So without further ado, here's my script:

Expand|Select|Wrap|Line Numbers
  1.  
  2. import os
  3. tralla='/windows'
  4. tintin='/LOLOL'
  5. list1=[]
  6. list2=[]
  7.  
  8. for item in os.listdir(tralla):
  9.     if os.path.splitext(item)[1][0]=='.':
  10.         list1.append(item)
  11.     else:
  12.         list2.append(item)
  13.  
  14.  
  15. for item in os.listdir(tintin):
  16.     if os.path.splitext(item)[1][0]=='.':
  17.         list1.append(item)
  18.     else:
  19.         list2.append(item)
  20.  
  21. for item in list2:
  22.     print item
  23.  
  24.  
So can anyone tell me why on earth I get this:
"Traceback (most recent call last):
File "untitled.py", line 8, in <module>
if os.path.splitext(item)[1][0]=='.':
IndexError: string index out of range"
?

At least this time it's with tab-indents, some people didn't like it when I used spaces last time... :)
Any help I can get is appreciated.
Feb 29 '08 #1
4 1402
dazzler
75
what does "print os.path.splitext(item)" look like?
Feb 29 '08 #2
dazzler
75
hmmh, just tried this and it worked for me

oh no it didn't...
Feb 29 '08 #3
dazzler
75
ok, if I have file called "001png"
and print os.path.splitext(item) prints ('001png', '')
where [0] is '001png'
and [1] = '' ,
[1][0] would be the first character in [1] but [1] is empty so [1][0] doesn't even exist

instead of
Expand|Select|Wrap|Line Numbers
  1. if os.path.splitext(item)[1][0] =='.' 
  2.  
use
Expand|Select|Wrap|Line Numbers
  1. if os.path.splitext(item)[1] !=''  #if [1] is anything else than empty  
  2.  
:)
Feb 29 '08 #4
ok, if I have file called "001png"
and print os.path.splitext(item) prints ('001png', '')
where [0] is '001png'
and [1] = '' ,
[1][0] would be the first character in [1] but [1] is empty so [1][0] doesn't even exist

instead of
Expand|Select|Wrap|Line Numbers
  1. if os.path.splitext(item)[1][0] =='.' 
  2.  
use
Expand|Select|Wrap|Line Numbers
  1. if os.path.splitext(item)[1] !=''  #if [1] is anything else than empty  
  2.  
:)
Anku! (mulefa for thank you!)
I wonder why I didn't think of that, that is brilliant!
Mar 2 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Ishwor | last post by:
i am trying to remove an item 'e' from the list l but i keep getting IndexError. I know the size of the list l is changing in the for loop & its sort of trivial task but i found no other way than...
5
by: shama.bell | last post by:
Hello, I am getting the following error when returning a list: return tbl "IndexError: each subindex must be either a slice, an integer, Ellipsis, or NewAxis" What does it mean?
7
by: Derek Schuff | last post by:
I'm sorry if this is a FAQ or on an easily-accesible "RTFM" style page, but i couldnt find it. I have some code like this: for line in f: toks = line.split() try: if int(toks,16) ==...
35
by: erikwickstrom | last post by:
Hi all, I'm sorry about the newbie question, but I've been searching all afternoon and can't find the answer! I'm trying to get this bit of code to work without triggering the IndexError. ...
7
by: python101 | last post by:
My code s= for k in range(len(s)): st=s.count(s) if st>=2: print s.pop(s.index(s)) Traceback (most recent call last):
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.