带导入模块的Python索引器

带导入模块的Python索引器,python,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

我有下面的脚本,它在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
#/usr/bin/python
#Python代码只是列出passwd文件中的信息,以从中获得不同的属性,例如:用户名、GUID、UID、HomeDir、Shell等。
#我们将使用文件处理来获得所需的数据。
#我们使用“Myfh”作为文件处理程序,open()函数打开并返回一个文件句柄,该句柄可用于以常规方式读取或写入文件。
#这里我们有'/tmp/passwd'文件从中获取数据&'r'是只读选项。
#我们使用split Function从文件中拆分“:”并选择所需的字段。
Myfh=打开('/tmp/passwd',r')
对于已排序的行(Myfh.readlines()):
a=line.strip().split(“:”)
“打印”用户名:,a[0]
打印“用户UID:”,a[2]
打印“用户GID:”,a[3]
打印“用户Gecos:”,a[4]
打印“用户HomeDir:”,a[5]
打印“用户Shell:”,a[6]
"""
打印“|%-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根根目录8304 2009年6月11日/usr/bin/python
#python labPasssort.py
回溯(最近一次呼叫最后一次):
文件“labPasssort.py”,第20行,在
打印“|%-17s |%-10s |%-10s |%-28s |%-24s |%-15s |”%(a[0]、a[2]、a[3]、a[4]、a[5]、a[6])
索引器:列表索引超出范围
============= 当我试图在python提示符“>>”上导入下面两个时,我觉得这不起作用。有人能解决这个问题吗

从操作系统导入listdir 从os.path导入isfile,加入


请帮助我,我是初学者。

你确定
a
的长度是7吗? 因为该错误意味着您正在尝试访问超出列表长度的项目


打印
a
查看实际长度。

感谢您的时间和分析。。。。虽然我发现“/tmp/passwd”文件本身有错误,因为最初的几行输出已损坏,无法读取,所以我得到了错误。。再次检索文件后,脚本工作正常…

尝试使用“除此之外尝试”捕获
索引器
并打印
a
以查看它包含的内容
您所说的
是什么意思我觉得这不起作用
?错误告诉您列表中的项目少于7项。如果您只是打印一个
,它会显示什么?您是缺少[1]
还是故意的?@Manish-我的意思是,当我在python提示符下运行这些命令时,如下所示。。在那之后它就不起作用了<代码>>>从os导入listdir
>从os.path导入isfile,加入“
@Brad..当我正在测试时,相同的脚本正在其他服务器上工作。如何测试它?长度是7。@Karn…我在上面的评论中告诉过你,在
a=line.strip().split(:)之后打印len(a)
。@Iron..它给出1,请参见下面的结果..
#python labPasssort.py 1回溯(最近一次调用):文件“labPasssort.py”,第20行,打印“|%-17s |%-10s |%-28s |%-24s |%-15s”%(a[0],a[2],a[3],a[4],a[5],a[6])索引器:列表索引超出范围。
。这意味着您的文件中有一行没有
。它可能是一个空行。我刚刚在另一台服务器上运行了脚本,它运行得非常好。
#python labpy.py | adm | 3 | 4 | adm |/var/adm |/sbin/nologin ||ais | 39 | 39 |基于openais标准的集群框架|/|/sbin/nologin | | | apache | 48 | apache |/var/www |/sbin/nologin | avahi autoipd | 100 | 156 | avahi autoipd |/var/lib/avahi-autoipd |/sbin/nologin |