我是Python新手,我試圖從ftp目錄獲取文件。每次運行此代碼時都會出現此錯誤。當我運行它時,它讀取目錄中的所有文件,然後在callback = mylist[1]
上給我一個錯誤。第一部分定義的功能,另一部分是我所說的文件:IndexError:從ftp讀取文件時字符串索引超出範圍
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
打印報表是你的朋友!在mylist in dirlist之後打印mylist:'你會看到它是'/',而不是一個列表。 – tdelaney
@tdelaney:如果字符串更長,則不會發生聲明的錯誤... –
@ScottHunter - 意思...,什麼?他會有同樣的錯誤,並且會在代碼中進一步發揮作用。打印語句仍然會顯示錯誤。 – tdelaney