Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器错误:从ftp读取文件时字符串索引超出范围_Python - Fatal编程技术网

Python 索引器错误:从ftp读取文件时字符串索引超出范围

Python 索引器错误:从ftp读取文件时字符串索引超出范围,python,Python,我是Python的新手,我正在尝试从ftp目录获取文件。每次运行此代码时都会出现此错误。当我运行此命令时,它读取目录中的所有文件,然后在callback=mylist[1]上给我一个错误。第一部分定义函数,另一部分调用文件: from ftplib import FTP import os, sys def rCallback3(filename): new_filename = "rrrr/files/%s" % just_filename retval =

我是Python的新手,我正在尝试从ftp目录获取文件。每次运行此代码时都会出现此错误。当我运行此命令时,它读取目录中的所有文件,然后在
callback=mylist[1]
上给我一个错误。第一部分定义函数,另一部分调用文件:

from ftplib import FTP
    import os, sys

    def rCallback3(filename):
    new_filename = "rrrr/files/%s" % just_filename
    retval = subprocess.call(["/usr/local/bin/r.py", filename])
    os.rename(filename, new_filename)
    return retval
我删除了一些代码

ftp = ftplib.FTP('192.192.0.195', 'ro', 'Password')
files = ftp.dir()
dirlist = ['/',rCallback3]

while (True):
    # Go through each of the directories
    for mylist in dirlist:
        check_dir = mylist[0]
        callback = mylist[1]-- here i get this error 

        # get the list of files in the directory
        filelist = os.listdir(check_dir)
        for this_file in filelist:
            if ((this_file == ".") or (this_file == "..") or (this_file == "donecsvfiles")or (this_file == "doneringofiles")):
                print "Skipping self and parent"
                continue

            full_filename = "%s/%s"%(check_dir, this_file)

            # Get the modification time of the file
            first_stat = os.stat(full_filename)[8]

            # Sleep for 1 second
            time.sleep(1)

            # Get the modification time again
            second_stat = os.stat(full_filename)[8]

            # If the modication time has not changed, then the file is stable
            # and can be sent to the callback
            if (first_stat == second_stat):
              callback(full_filename)
我现在得到这个错误

{"iv":
  { "result": "ok" }
}
Traceback (most recent call last):
  File "/usr/local/bin/ringo.py", line 51, in <module>
    reader = csv.reader(open(csvfile, 'r'))
IOError: [Errno 21] Is a directory: '//tmp'
Traceback (most recent call last):
  File "./dirmon.py", line 83, in <module>
    callback(full_filename)
  File "./dirmon.py", line 46, in ringoCallback3
    os.rename(filename, new_filename)
OSError: [Errno 18] Invalid cross-device link
{“iv”:
{“结果”:“确定”}
}
回溯(最近一次呼叫最后一次):
文件“/usr/local/bin/ringo.py”,第51行,在
reader=csv.reader(打开(csvfile,'r'))
IOError:[Errno 21]是一个目录:'//tmp'
回溯(最近一次呼叫最后一次):
文件“/dirmon.py”,第83行,在
回调(完整文件名)
文件“/dirmon.py”,第46行,在ringoCallback3中
重命名(文件名,新文件名)
OSError:[Errno 18]无效的跨设备链接

dirlist[0]
是单个字符串(
'/'
),因此
mylist
最初是该字符串;因为它只有一个字符,所以在
mylist[1]
中没有任何内容


也许你真的不想迭代
dirlist
,或者它应该是
[['/',rCallback3]]

在程序的开头是这样一行:

dirlist = ['/',rCallback3]
它将名称
dirlist
分配给列表
['/',ringoCallback3]
。然后有一个for循环:

for mylist in dirlist:
迭代
dirlist
,并将每个项目作为
mylist
生成

目录列表
中的第一项(因此
mylist
的第一个值)是字符串
'/'

因此,当Python第一次遇到这两行代码时:

check_dir = mylist[0]
callback = mylist[1]
它将其视为等同于:

check_dir = '/'[0]
callback = '/'[1]
第二个将引发一个
索引器
,因为
'/'
只有一个字符,但您告诉Python在索引
1
处获取一个不存在的第二个字符:

>>> '/'[1]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>>
>'/'[1]
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
索引器错误:字符串索引超出范围
>>>

Print语句是您的朋友!在dirlist:
中的mylist的
之后打印mylist,您会看到它是“/”,而不是一个列表。@tdelaney:如果字符串较长,则不会出现声明的错误…@ScottHunter-意思是…,什么?他也会有同样的错误,而且会进一步影响代码。print语句仍然会显示错误。谢谢icodez,但我只有一个目录,其中有多个文件。当我使用callback=mylist[0]时,我会得到回溯(最近一次调用last):callback(full_filename)TypeError中第83行的文件“/dirmon.py”:“str”对象不可调用