简体   繁体   English

当文件不存在时忽略 ftplib 的 550 错误并继续处理其他文件

[英]Ignore ftplib's 550 error when file does not exist and and continue with other files

I have problem with ignoring an error and just going on with other commands.我在忽略错误并继续执行其他命令时遇到问题。 The code below is checking if file exists (using FTP.size ) and prints message if so.下面的代码检查文件是否存在(使用FTP.size ),如果存在则打印消息。 And - here is problem.而且 - 这是问题。 When it doesn't find file in this main directory, it should go to /folder2 and look for file here.当它在这个主目录中找不到文件时,它应该去/folder2并在这里寻找文件。 Instead it throws traceback error (which I "silenced") and doesn't go to second folder...相反,它抛出回溯错误(我“沉默”)并且不去第二个文件夹......

from ftplib import FTP
import sys
sys.tracebacklimit = 0


ftp = FTP(host="ip")
loginRepsonse = ftp.login(user='name', passwd='password')
print(loginRepsonse)

fileName = "2022121414040114FTPtestLarge.txt"
# fileName = str(input('What is name of your file? '))

size = ftp.size(fileName)


if isinstance(size, int):
    print("File exists!")
else :    
    ftp.cwd('/folder2')
    if isinstance(size, int):
        print("File exists in another directory!")

When file exists it's all cool, it shows that file indeed exist, but when not... This error below appears and execution ends, without looking to second folder...当文件存在时一切都很好,它表明文件确实存在,但是当不存在时......出现下面的错误并且执行结束,而不看第二个文件夹......

ftplib.error_perm: 550 Could not get file size. ftplib.error_perm: 550 无法获取文件大小。

My FTP has only "main" directory and one subfolder - code needs to check two folders (main, then 2nd folder if file wasn't found).我的 FTP 只有“主”目录和一个子文件夹——代码需要检查两个文件夹(主文件夹,如果找不到文件,则检查第二个文件夹)。 How to ignore this error and continue searching?如何忽略此错误并继续搜索?

Below is whole error output with non-existing file (same happens for EXISTING file, but in different directory, which I can't access):下面是不存在文件的整个错误输出(现有文件也会发生同样的情况,但在不同的目录中,我无法访问):

230 Login successful.
Traceback (most recent call last):
  File "c:\Users\user\Desktop\CODES+SCRIPTS\ftp.py", line 13, in <module>
    size = ftp.size(fileName)
           ^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\ftplib.py", line 630, in size
    resp = self.sendcmd('SIZE ' + filename)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\ftplib.py", line 281, in sendcmd
    return self.getresp()
           ^^^^^^^^^^^^^^
  File "C:\Users\user\AppData\Local\Programs\Python\Python311\Lib\ftplib.py", line 254, in getresp
    raise error_perm(resp)
ftplib.error_perm: 550 Could not get file size.

Below is another code I tried:下面是我试过的另一个代码:

size = ftp.size(fileName)

from ftplib import FTP, error_perm
try:
    size = ftp.size(fileName)
    print(f"File exists {size}")
    ftp.cwd('/test')          #checks subfolder(or it should..)
    if isinstance(size, int):
         print("File exists in another directory!")
except error_perm as e:
    print(f"File does not exist anywhere: {e}")

This is working (not finished) code based on the answer below.这是基于以下答案的工作(未完成)代码。

from ftplib import FTP, error_perm

ftp = FTP(host=host)
loginRepsonse = ftp.login(user=user, passwd=passwd)
print(loginRepsonse)

def repeat():
    fileName = str(input('>>> What is name of your file? \n'))
    try:
        size = ftp.size(fileName)
        print(f"File exists in main directory.")
    except error_perm as e:
        pass
        print(f"File does not exist in main directory.")

    try:
        ftp.cwd('/testLAB')
        size = ftp.size(fileName)
        if isinstance(size, int):
                print("File exists in subfolder!")
    except:
        print(f"File does not exist anywhere yet.")

while True:
   repeat()

Just catch the appropriate exception:只需捕获适当的异常:

from ftplib import FTP, error_perm
 
# ...

try:
    size = ftp.size(fileName)
    print(f"File exists {size}")
except error_perm as e:
    print(f"File does not exist (or other error): {e}");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ftplib.error_perm:尝试更改目录时出现 550 - ftplib.error_perm: 550 when trying to change directory 尝试使用 FTP_TLS 从同一目录下载第二个文件时出现“ftplib.error_perm: 550 Operation not allowed” - "ftplib.error_perm: 550 Operation not permitted" when trying to download the second file from the same directory using FTP_TLS c#:尝试下载文件时出现随机FTP错误(550) - c# : Random FTP error (550) when trying to download files 使用Python ftplib下载时忽略丢失的文件 - Ignore missing file while downloading with Python ftplib 使用ftplib监视FTP服务器中的新文件时,是否可以查看是否仍在传输新文件? - When monitoring a FTP server with ftplib for new files, is there a way to see if a new file is still being transferred? 多个线程将文件上传到Filezilla ftp服务器返回错误550文件不可用 - Multiple threads uploading files to filezilla ftp server returns error 550 File unavailable 检查FTPS上是否存在文件,错误550 - Check if file exists on FTPS, Error 550 FTP fopen()错误550-不是纯文件 - FTP fopen() error 550—not a plain file FtpWebRequest 返回错误 550 文件不可用 - FtpWebRequest returns error 550 File unavailable 上载文件时检索文件大小时出现550错误 - 550 error on retrieving filesize while uploading file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM