通过二进制或 ASCII 上传文本文件都会将其留空吗?

发布于 2025-01-06 19:23:57 字数 2678 浏览 1 评论 0原文

所以我一直在尝试制作一个插件来告诉另一端下载FTP服务器上文本文件中的链接,并且我能够接收服务器上的当前版本并对其进行修改,但是当我尝试重新上传时如果文本文件显示为空白。我尝试使用 FTP.storline 和 FTP.storbinary ,两者都给出了相同的结果。我已将打印函数放入回调中,以查看是否发生了任何事情。无论如何,任何关于为什么我无法发送带有所有附加数据的文件的帮助都将非常好:D!

-Clement

代码:

def upload(link,ip,username,passwd):
    present = False
    connection = ftplib.FTP(ip)
    connection.login(user=username,passwd=passwd)
    items = connection.nlst()
    for x in items:
        if x == "list.txt":
            present = True
            break
    if present == True:
        username = os.getlogin()
        print("Got login!")
        locallist_dir = "/Users/" + username
        locallist = locallist_dir + "/list.txt"

        opened_llist_r = open(locallist, "rb")
        opened_llist = open(locallist, "wt")
        print("Opened file in", locallist)

        connection.retrlines("RETR %s" % "list.txt", opened_llist.write)
        print("Added lines from FTP")

        opened_llist.write(link + " ")
        print("Link:","'",link,"'","written!")
        print(opened_llist," | ",opened_llist_r)

        connection.storbinary("STOR %s" % "list.txt", opened_llist_r, 8192, print)
        print("Re-uploaded!")



        opened_llist.close()
        opened_llist_r.close()
    else:
        print("Your current Connection does not have the list.txt file.")

    connection.close()
    print("Connection Closed.")

已回答:

抱歉这个愚蠢的问题,正如 Vaughn Cato 指出的那样,我不应该同时打开两个。我修复了我的代码,在调用下一个文件之前关闭第一个打开的文件。最终代码如下所示:

def upload(link,ip,username,passwd):
    present = False
    connection = ftplib.FTP(ip)
    connection.login(user=username,passwd=passwd)
    items = connection.nlst()
    for x in items:
        if x == "list.txt":
            present = True
            break
    if present == True:
        username = os.getlogin()
        print("Got login!")
        locallist_dir = "/Users/" + username
        locallist = locallist_dir + "/list.txt"

        opened_llist = open(locallist, "wt")
        print("Opened file in", locallist)

        connection.retrlines("RETR %s" % "list.txt", opened_llist.write)
        print("Added lines from FTP")

        opened_llist.write(link + " ")
        print("Link:","'",link,"'","written!")
        print(opened_llist)
        opened_llist.close()

        opened_llist_r = open(locallist, "rb")
        connection.storbinary("STOR %s" % "list.txt", opened_llist_r, 8192, print)
        print("Re-uploaded!")



        opened_llist_r.close()
    else:
        print("Your current Connection does not have the list.txt file.")

    connection.close()
    print("Connection Closed.")        

So I have been trying to a make a plugin to tell the other end to download links in a text file on a FTP server and I am able to receive the current version on the server and modify it, but when I try to re-upload it the text file appears blank. I have tried using FTP.storline and FTP.storbinary with both giving me the same result. I've put the print function in the callback to see if anything it is happening which it isn't. Anyways, any help as to why I am unable to send my file with all the data attached would be excellent :D!

-Clement

CODE:

def upload(link,ip,username,passwd):
    present = False
    connection = ftplib.FTP(ip)
    connection.login(user=username,passwd=passwd)
    items = connection.nlst()
    for x in items:
        if x == "list.txt":
            present = True
            break
    if present == True:
        username = os.getlogin()
        print("Got login!")
        locallist_dir = "/Users/" + username
        locallist = locallist_dir + "/list.txt"

        opened_llist_r = open(locallist, "rb")
        opened_llist = open(locallist, "wt")
        print("Opened file in", locallist)

        connection.retrlines("RETR %s" % "list.txt", opened_llist.write)
        print("Added lines from FTP")

        opened_llist.write(link + " ")
        print("Link:","'",link,"'","written!")
        print(opened_llist," | ",opened_llist_r)

        connection.storbinary("STOR %s" % "list.txt", opened_llist_r, 8192, print)
        print("Re-uploaded!")



        opened_llist.close()
        opened_llist_r.close()
    else:
        print("Your current Connection does not have the list.txt file.")

    connection.close()
    print("Connection Closed.")

ANSWERED:

Sorry for the silly question, as Vaughn Cato pointed out I should not have two open at the same time. I fixed my code be closing the first file opening before I call the next one. The final code looks like this:

def upload(link,ip,username,passwd):
    present = False
    connection = ftplib.FTP(ip)
    connection.login(user=username,passwd=passwd)
    items = connection.nlst()
    for x in items:
        if x == "list.txt":
            present = True
            break
    if present == True:
        username = os.getlogin()
        print("Got login!")
        locallist_dir = "/Users/" + username
        locallist = locallist_dir + "/list.txt"

        opened_llist = open(locallist, "wt")
        print("Opened file in", locallist)

        connection.retrlines("RETR %s" % "list.txt", opened_llist.write)
        print("Added lines from FTP")

        opened_llist.write(link + " ")
        print("Link:","'",link,"'","written!")
        print(opened_llist)
        opened_llist.close()

        opened_llist_r = open(locallist, "rb")
        connection.storbinary("STOR %s" % "list.txt", opened_llist_r, 8192, print)
        print("Re-uploaded!")



        opened_llist_r.close()
    else:
        print("Your current Connection does not have the list.txt file.")

    connection.close()
    print("Connection Closed.")        

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

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

发布评论

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

评论(1

坦然微笑 2025-01-13 19:23:57

打开同一个文件两次是危险的,并且可能会导致意外的行为。像这样的东西更好:

opened_llist = open(locallist, "wt")
connection.retrlines("RETR %s" % "list.txt", opened_llist.write)
opened_llist.write(link + " ")
opened_llist.close()
opened_llist_r = open(locallist, "rb")
connection.storbinary("STOR %s" % "list.txt", opened_llist_r, 8192, print)
opened_llist_r.close()

Having the same file opened twice is dangerous and can lead to unexpected behaviour. Something like this is better:

opened_llist = open(locallist, "wt")
connection.retrlines("RETR %s" % "list.txt", opened_llist.write)
opened_llist.write(link + " ")
opened_llist.close()
opened_llist_r = open(locallist, "rb")
connection.storbinary("STOR %s" % "list.txt", opened_llist_r, 8192, print)
opened_llist_r.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文