ftp.cwd()我不明白错误

时间:2017-09-24 21:48:23

标签: python ftp

当我运行此代码时:

for i in range(len(ftp_folders)): #ftp_folders[] list

    ftp.cwd(ftp_folders[i]) #first interaction __> ftp.folders[Templates]
    print(ftp.pwd()) #entering int the folder 
    print('entering in : ',ftp_folders[i]) # 1. Templates

    ftp.cwd(ftp_folders[i]) #ERROR WHY??
    ftp.retrlines('LIST')

for循环在第一次交互中崩溃。 我可以输入ftp_folders [i](模板)的第一个文件夹,但是当运行ftp.cwd()时,它会崩溃

错误:

/home/pi/Templates

输入:模板

Traceback (most recent call last):
  File "C:\Users\peces\Documents\python\exercises\ftp\ping.pyw", line 47, in <module> ftp.cwd(ftp_folders[i])
File "C:\Users\peces\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 629, in cwd
    return self.voidcmd(cmd)
File "C:\Users\peces\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 276, in voidcmd
    return self.voidresp()
  File "C:\Users\peces\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 249, in voidresp
    resp = self.getresp()
  File "C:\Users\peces\AppData\Local\Programs\Python\Python36-32\lib\ftplib.py", line 244, in getresp
    raise error_perm(resp)
ftplib.error_perm: 550 Templates: No such file or directory

没有这样的文件或目录?我不能解释。确定问题是其他.. ftp.cwd(ftp_folders [i])运行错误..并且要么知道原因。

一些帮助?

2 个答案:

答案 0 :(得分:0)

您目前执行以下操作:

  • 将远程目录更改为Templates

    • 因为它是相对路径,这会相对于您登录时的目录更改目录。
  • 打印您的工作目录;

  • 您现在再次尝试cwd Templates

    • 这是一个相对路径,您正在尝试更改到您已经在的Templates目录的Templates子目录。

请注意,CWD路径的格式取决于实现,但通常支持:

  • /开头的路径表示绝对路径;而
  • 路径/开头,表示相对于当前PWD的路径

答案 1 :(得分:-1)

好吧,我明白了。

在迭代结束时我不得不写:ftp.cwd('../')以便返回目录。写下我可以回到目录并列出“base”文件夹的每个文件夹:/ home / pi。

结果,再添加更多代码,它说:

for i in range(len(ftp_folders)):
    print('print before change directory')
    print(ftp.cwd(ftp.pwd()+'/'+ftp_folders[i]))
    print(ftp.pwd())
    print('entering in : ',ftp_folders[i])
    #ftp.cwd('Templates')
    #ftp.cwd(ftp_folders[i])
    ftp.retrlines('LIST')
    ftp.cwd('../')

现在它运行了。! 非常感谢@donkopotamus