AttributeError:'模块'对象没有属性' nlst'

时间:2015-06-22 16:39:25

标签: python ftplib

我试图把它放到def语句中:

#!/usr/bin/env python

import ftplib
import os
import sys
import paramiko
import datetime
import pickle

username = "ftp1"
password = "pass"


try:
        try:
            print "Connecting to 0.0.0.0"
            ftp = ftplib.FTP("0.0.0.0")
            ftp.login(username, password)
            ftp.cwd('Dir')

        except ftplib.all_errors as e:
            print(e)
except ftp.login as s:
    print (s)

files = []

try:
        files = ftp.nlst()
except ftplib.error_perm, resp:
   if str(resp) == "550 No files found":
        print "No files in this directory"
   else:
        raise

for f in files:
   print f

----------------------订正------------------------ -

#!/usr/bin/env python

import ftplib
import os
import sys
import paramiko
import datetime
import pickle
import ftplib as ftp


hostname = "0.0.0.0"
password = "pass"
username = "ftp1"

def connect():
    try:
        print "Connecting to 0.0.0.0"
        ftp = ftplib.FTP("0.0.0.0")
        ftp.login(username, password)

    except ftplib.all_errors as e:
        print(e)

connect()


def list_files():

    files = []

    try:
             files = ftp.nlst()
    except ftplib.error_perm, resp:
        if str(resp) == "550 No files found":
            print "No files in this directory"
        else:
             raise

    for f in files:
       print f

list_files()

创建第一个函数成功:

def connect():
    try:
        print "Connecting to 0.0.0.0"
        ftp = ftplib.FTP("0.0.0.0")
        ftp.login(username, password)

    except ftplib.all_errors as e:
        print(e)

connect()

-----------------------在第二个def语句中得到错误----------------

def list_files():

    files = []

    try:
             files = ftp.nlst()
    except ftplib.error_perm, resp:
        if str(resp) == "550 No files found":
            print "No files in this directory"
        else:
             raise

    for f in files:
       print f

list_files()

==================== AttributeError:' module'对象没有属性 ' ============

请帮助我理解我在这里缺少的东西......我确信它很简单......但是我想念它....

2 个答案:

答案 0 :(得分:0)

import ftplib as ftp之后,ftp引用了ftplib模块。由于您直接使用ftplib,因此您应该从导入语句中删除as ftp

此外,由于您要致电nlst,因此您必须将实际的ftp变量传递给list_files

答案 1 :(得分:0)

想要感谢所有发布答案的人帮助我纠正错误。 能够找出“Arkanosis”建议修复错误的内容:

你有一个选择是让connect返回ftp变量,然后将它作为参数传递给list_files函数。然后用法看起来像ftp = connect()然后是list_files(ftp)。另一种方法是将两个函数放在一个将ftp作为属性的类中。 - Arkanosis

ftp = ftplib.FTP(“0.0.0.0”)

ftp.login(用户名,密码)

将ftp添加到每个“def”块中:

#!/usr/bin/env python

import ftplib
import os
import sys
import paramiko
import datetime
import pickle
import ftplib as ftp


hostname = "0.0.0.0"
password = "pass"
username = "ftp"


def connect():
    try:
        print "Connecting to 0.0.0.0"
        ftp = ftplib.FTP("0.0.0.0")
        ftp.login(username, password)

    except ftplib.all_errors as e:
        print(e)

connect()

def list_files():
    try:
           ftp = ftplib.FTP("0.0.0.0")
           ftp.login(username,password)
           files = []
           files = ftp.nlst()
    except ftplib.error_perm, resp:
       if str(resp) == "550 No files found":
            print "No files in this directory"
       else:
            raise

    for f in files:
       print f

list_files()

def list_directory():
    try:
          ftp = ftplib.FTP("0.0.0.0")
          ftp.login(username, password)
          files = []
          files = ftp.retrlines('LIST')
    except ftplib.error_perm, resp:
       if str(resp) == "550 No files found":
            print "No files in this directory"
       else:
            raise

    for f in files:
        print file

list_directory()

def list_Upload():
    try:
          ftp = ftplib.FTP("0.0.0.0")
          ftp.login(username, password)
          ftp.cwd('directory')
          files = []
          files = ftp.retrlines('LIST')
    except ftplib.error_perm, resp:
       if str(resp) == "550 No files found":
            print "No files in this directory"
       else:
            raise

    for f in files:
        print file

list_Upload()

我将在即将发布的版本中将其转换为类声明,并将为那些希望将其用作模板以帮助构建其ftp项目的人更新帖子。

这是构建最终FTP脚本时开始与FTP服务器通信的非常基本的构建。