簡體   English   中英

如果斷斷續續的if語句,無法弄清楚為什么這行不通

[英]Broken if statement, can't figure out why this won't work

即使目錄確實存在於列表中,代碼也會不斷卡住。 我不確定這是怎么回事。 當我注釋掉編寫的代碼以重新向用戶詢問目錄(如果該目錄不存在)時,該代碼可以正常工作。

def path_sel():
path = raw_input("Select desired working directory: ")
b = []
ftp.retrlines('LIST', b.append)
if path not in b:
    print "ERROR- Directory does not exist.\n"
    path_sel()
else:
    print '\nChanging to '+ path, '\n'
    ftp.cwd(path)
    print path
    ftp.retrlines('LIST')
    c = []
    ftp.retrlines('LIST', c.append)
    if 'd' in str(c[0]):
            path_sel()
file_dl()

這似乎對我有用(我選擇了一個隨機FTP服務器):

from ftplib import FTP 

def path_sel():
   path = raw_input("Select desired working directory: ")
   b = []
   ftp.retrlines('LIST', b.append)

   found = False
   for entry in b:
      if path in entry:
         found = True

   if not found:
      print "ERROR- Directory does not exist.\n"
      path_sel()
   else:
      print '\nChanging to '+ path, '\n'
      ftp.cwd(path)
      print path
      ftp.retrlines('LIST')
      # After printing the directory, you probably
      # want to ask the user to enter a new file/path
      # rather than just assuming that they are interested
      # in the first entry in the ftp.retrlines() printout
      c = []
      ftp.retrlines('LIST', c.append)
      if 'd' in str(c[0]):
         path_sel()
   # ---------------------------------------------------
   # Once you've extracted a filename, run your function
   # ---------------------------------------------------
   print 'Running file_dl()'
   #file_dl(filename)

ftp = FTP('ftp.debian.org')
ftp.login()
path_sel()

它不斷返回“錯誤-目錄不存在”,並要求輸入新的內容,直到輸入有效的路徑為止。 一旦找到文件(不是以'drwxrwxrwx ....開頭的文件),就想執行file_dl()函數。

這是我的控制台中的樣子:

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM