带有导入模块的Python IndexError

时间:2015-12-10 14:09:50

标签: python

我的下面的脚本在pyhthon 2.7上运行得非常好,直到日期为止,

#!/usr/bin/python
# Python code Just to list the information from passwd file to obtain diffrent feilds out of it eg: UserName,GUID,UID,HomeDir,Shell etc.
# We will be using File-handling to obtain the Desired data.
# We have used 'Myfh' as a File-handler ,The open() function opens and returns a file handle that can be used to read or write a file in the usual way.
# Here we have '/tmp/passwd' file to get the data from it & 'r' is read-Only option to do it.
# We either used split fucntion to split the ":" from the file and opt for desired feild.

Myfh = open('/tmp/passwd', 'r')

for line in sorted(Myfh.readlines()):
        a = line.strip().split(":")
        """print "User Name: ", a[0]
        print 'User UID: ', a[2]
        print 'User GID: ', a[3]
        print 'User Gecos: ', a[4]
        print 'User HomeDir: ', a[5]
        print 'Users Shell: ', a[6]
        """
        print '| %-17s |%-10s | %-10s | %-28s | %-24s | %-15s |' % (a[0],a[2],a[3],a[4],a[5],a[6])

# ls -l /usr/bin/python
-rwxr-xr-x 2 root root 8304 Jun 11  2009 /usr/bin/python

# python  labPasssort.py
Traceback (most recent call last):
  File "labPasssort.py", line 20, in <module>
    print '| %-17s |%-10s | %-10s | %-28s | %-24s | %-15s |' % (a[0],a[2],a[3],a[4],a[5],a[6])
IndexError: list index out of range

============= 当我尝试在python提示符下导入以下两个“&gt;&gt;”时,我觉得这不起作用..有人可以搞清楚......

  
    来自os import listdir的

    从os.path import isfile,join

  

请帮助我成为初学者..

2 个答案:

答案 0 :(得分:0)

你确定 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/a" android:layout_width="match_parent" android:layout_height="match_parent"> <RelativeLayout android:layout_width="match_parent" android:layout_below="@+id/ad_view_chat" android:layout_gravity="bottom" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listChatLayout" android:layout_marginBottom="8dp" android:layout_marginLeft="8dp" android:layout_marginRight="8dp" android:layout_above="@+id/switchbuttons" android:layout_marginTop="2dp"> <ListView android:id="@+id/listChat" android:layout_gravity="bottom" android:layout_width="match_parent" android:layout_height="wrap_content" android:longClickable="true" android:stackFromBottom="true" android:transcriptMode="normal" /> </LinearLayout> <RelativeLayout android:id="@+id/switchbuttons" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" android:layout_above="@+id/messaggiLayout" android:layout_centerHorizontal="true"> <RadioButton android:id="@+id/cmd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="15dp" android:checked="false" android:gravity="center" android:text="Cmd" android:layout_marginLeft="5dp" /> <RadioButton android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="25dp" android:layout_toRightOf="@+id/cmd" android:checked="true" android:gravity="center" android:text="Message" /> <com.rey.material.widget.Button android:id="@+id/button_arrow_down" android:background = "@drawable/arrow_down" style="@style/FlatColorButtonRippleStyle" android:layout_width="40dp" android:layout_height="40dp" android:text="" android:textAppearance="@style/Base.TextAppearance.AppCompat.Button" android:textColor="#FF2196F3" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/button_arrow_up" android:layout_toEndOf="@+id/button_arrow_up" android:layout_marginLeft="3dp" /> <com.rey.material.widget.Button android:id="@+id/button_arrow_up" style="@style/FlatColorButtonRippleStyle" android:layout_width="40dp" android:layout_height="40dp" android:background = "@drawable/arrow_up" android:text="" android:textAppearance="@style/Base.TextAppearance.AppCompat.Button" android:textColor="#FF2196F3" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/message" android:layout_toEndOf="@+id/message" android:layout_marginLeft="20dp" /> </RelativeLayout> <RelativeLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/listChatLayout" android:layout_alignParentBottom="true" android:id="@+id/messaggiLayout"> <EditText android:id="@+id/messaggi" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="5dp" android:imeOptions="actionSend|flagNoFullscreen" android:inputType="textImeMultiLine" android:layout_toLeftOf="@+id/button_send" android:maxLength="100"> <requestFocus /> </EditText> <com.rey.material.widget.Button android:id="@+id/button_send" style="@style/FlatColorButtonRippleStyle" android:layout_width="50dp" android:layout_alignParentRight="true" android:background = "@drawable/send2" android:layout_height="50dp" android:text="" android:textAppearance="@style/Base.TextAppearance.AppCompat.Button" android:textColor="#FF2196F3" /> </RelativeLayout> </RelativeLayout> <com.google.android.gms.ads.AdView android:id="@+id/ad_view_chat" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" ads:adSize="SMART_BANNER" ads:adUnitId="@string/banner_chat_ad_unit_id" /> </RelativeLayout> 的长度是7吗? 因为该错误意味着您正在尝试访问超出列表长度的项目。

打印a以查看实际长度。

答案 1 :(得分:0)

感谢您的时间和分析....虽然我收到了错误,因为我发现错误为“/ tmp / passwd”文件本身,因为最初的几行是损坏的输出无法读取它...检索后一个文件再次正常工作...