python ftplib -忽略lib的550错误并继续文件搜索

tquggr8v  于 2022-12-17  发布在  Python
关注(0)|答案(1)|浏览(183)

我对python还是个新手,我知道我的代码看起来很难看。我有忽略错误的问题(traceback)并继续执行命令。下面的代码检查文件是否存在(使用ftp.size)并打印消息,如果是的话。这里有一个问题。当它在这个主目录中找不到文件时,它应该转到/folder 2并在这里查找文件。相反,它抛出了回溯错误(我将其“静音”),并且没有转到第二个文件夹...

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!")

while True:
   pass

当文件存在时,这一切都很酷,它表明文件确实存在,但当不存在时...下面的错误出现,执行结束,而不看第二个文件夹...

ftplib.error_perm: 550 Could not get file size.

请注意这个

while True:
   pass

只是为了让我的脚本(在cmd中)保持活跃,不要在写入名称之前随机关闭输入😁我的FTP只有“主”目录和一个子文件夹-代码需要检查两个文件夹(主,然后第二个文件夹,如果文件没有找到).如何忽略这个错误并继续搜索?
下面是整个错误输出与不存在的文件(同样的情况发生在现有的文件,但在不同的目录,我无法访问):

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.

下面是我尝试的另一个代码:

from ftplib import FTP

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

fileName = "2022121608003014FTPtest.txt"   #this file exists in 2nd folder

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}")

以下是工作(未完成)代码:

from ftplib import FTP
from ftplib import FTP, error_perm

ftp = FTP(host="68.183.3.24")
loginRepsonse = ftp.login(user='sumeyra', passwd='Pysense2020!')
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()
flvlnr44

flvlnr441#

只需捕获适当的异常:

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}");

相关问题