FTP和Python的问题

发布于 2024-09-07 01:33:02 字数 1383 浏览 8 评论 0原文

有人可以帮助我吗?

为什么它不起作用

import ftplib
import os

def readList(request):
         machine=[]
         login=[]
         password=[]
         for line in open("netrc"): #read netrc file
            old=line.strip()
            line=line.strip().split()
            if old.startswith("machine"): machine.append(line[-1])
            if old.startswith("login"): login.append(line[-1])
            if old.startswith("password"): password.append(line[-1])
            connectFtp(machine,login,password)

def connectFtp(machine,login,password):
  for i in range(len(machine)):
          try:
             ftp = ftplib.FTP(machine[i])
             print 'conected to ' + machine[i]
             ftp.login(login[i],password[i])
             print 'login - ' + login[i] + ' pasword -' + password[i]
           except Exception,e:
             print e
           else:
       ftp.cwd("PublicFolder")
    print 'PublicFolder'

def upload(filename, file):
       readList()
          ext = os.path.splitext(file)[1]
            if ext in (".txt", ".htm", ".html"):
            ftp.storlines("STOR " + filename, open(file))
            else:
             ftp.storbinary("STOR " + filename, open(file, "rb"), 1024)
             print 'success... yra'

upload('test4.txt', r'c:\example2\media\uploads\test4.txt')`

当它们在一起时它起作用了。但是当我将它分成函数时发生了一些事情,我无法理解是什么。

Can someone help me.

Why it is not working

import ftplib
import os

def readList(request):
         machine=[]
         login=[]
         password=[]
         for line in open("netrc"): #read netrc file
            old=line.strip()
            line=line.strip().split()
            if old.startswith("machine"): machine.append(line[-1])
            if old.startswith("login"): login.append(line[-1])
            if old.startswith("password"): password.append(line[-1])
            connectFtp(machine,login,password)

def connectFtp(machine,login,password):
  for i in range(len(machine)):
          try:
             ftp = ftplib.FTP(machine[i])
             print 'conected to ' + machine[i]
             ftp.login(login[i],password[i])
             print 'login - ' + login[i] + ' pasword -' + password[i]
           except Exception,e:
             print e
           else:
       ftp.cwd("PublicFolder")
    print 'PublicFolder'

def upload(filename, file):
       readList()
          ext = os.path.splitext(file)[1]
            if ext in (".txt", ".htm", ".html"):
            ftp.storlines("STOR " + filename, open(file))
            else:
             ftp.storbinary("STOR " + filename, open(file, "rb"), 1024)
             print 'success... yra'

upload('test4.txt', r'c:\example2\media\uploads\test4.txt')`

When it was together it was working. But when i separate it in to functions something happened, I cant understand what.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

菊凝晚露 2024-09-14 01:33:02

(除了可怕的缩进问题,这可能是由于复制和粘贴失败造成的,否则你会得到语法错误......!)......:

范围问题,首先:connectFtp使得一个本地变量ftp,以便变量在函数完成后立即消失。然后 upload 尝试使用该变量,但当然它不再存在了。

connectFtp 末尾添加一个 return ftp,一个 yield connectFtp,而不是对 readList 中循环的简单调用>,并在 upload 中使用 for ftp in readList(): 循环。

(Apart from the horrid indentation problems, which are presumably due to botched copy and paste otherwise you'd get syntax errors up the wazoo...!)...:

Scoping problem, first: connectFtp makes a local variable ftp so that variables goes away as soon as the function's done. Then upload tries using the variable, but of course it isn't there any more.

Add a return ftp at the end of connectFtp, a yield connectFtp instead of a plain call to the loop in readList, and use a for ftp in readList(): loop in upload.

甜味超标? 2024-09-14 01:33:02

像这样的东西吗?

import os


def readList(request):
    machine = []
    login = []
    password = []
    for line in open("netrc"):  # read netrc file
        old = line.strip()
        line = line.strip().split()
        if old.startswith("machine"): machine.append(line[-1])
        if old.startswith("login"): login.append(line[-1])
        if old.startswith("password"): password.append(line[-1])
        yield connectFtp


def connectFtp(machine, login, password):
    for i in range(len(machine)):


try:
    ftp = ftplib.FTP(machine[i])
    print 'conected to ' + machine[i]
    ftp.login(login[i], password[i])
    print 'login - ' + login[i] + ' pasword -' + password[i]
except Exception, e:
    print e
else:
    ftp.cwd("PublicFolder")
    print 'PublicFolder'
    return (ftp)


def upload(filename, file):
    for ftp in readList():
        ext = os.path.splitext(file)[1]
    if ext in (".txt", ".htm", ".html"):
        ftp.storlines("STOR " + filename, open(file))
    else:
    ftp.storbinary("STOR " + filename, open(file, "rb"), 1024)
    print 'success... yra'

upload('test4.txt', r'c:\example2\media\uploads\test4.txt')

第 19 行发生错误 try:
unindent 不计算任何外部缩进级别

Something like this?

import os


def readList(request):
    machine = []
    login = []
    password = []
    for line in open("netrc"):  # read netrc file
        old = line.strip()
        line = line.strip().split()
        if old.startswith("machine"): machine.append(line[-1])
        if old.startswith("login"): login.append(line[-1])
        if old.startswith("password"): password.append(line[-1])
        yield connectFtp


def connectFtp(machine, login, password):
    for i in range(len(machine)):


try:
    ftp = ftplib.FTP(machine[i])
    print 'conected to ' + machine[i]
    ftp.login(login[i], password[i])
    print 'login - ' + login[i] + ' pasword -' + password[i]
except Exception, e:
    print e
else:
    ftp.cwd("PublicFolder")
    print 'PublicFolder'
    return (ftp)


def upload(filename, file):
    for ftp in readList():
        ext = os.path.splitext(file)[1]
    if ext in (".txt", ".htm", ".html"):
        ftp.storlines("STOR " + filename, open(file))
    else:
    ftp.storbinary("STOR " + filename, open(file, "rb"), 1024)
    print 'success... yra'

upload('test4.txt', r'c:\example2\media\uploads\test4.txt')

Error at line 19 something with try:
unindent does not math any outer indentation level

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文