Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 下载整个目录(包含文件和子目录)_Python - Fatal编程技术网

Python 下载整个目录(包含文件和子目录)

Python 下载整个目录(包含文件和子目录),python,Python,我想在linux ftp服务器上下载包含所有文件和子文件夹/子子文件夹的特定目录 我找到的代码只适用于linux ftp服务器和linux操作系统,但我的操作系统是windows。我检查了代码,它只是复制了目录结构,所以用“\\”替换“/”应该可以完成这项工作。然而,我未能使代码正常工作 这是我的当前代码(非工作代码),我刚刚将path替换为path。在相关位置替换(“/”,“\\”: import sys import ftplib import os from ftplib import F

我想在linux ftp服务器上下载包含所有文件和子文件夹/子子文件夹的特定目录

我找到的代码只适用于linux ftp服务器和linux操作系统,但我的操作系统是windows。我检查了代码,它只是复制了目录结构,所以用
“\\”
替换
“/”
应该可以完成这项工作。然而,我未能使代码正常工作

这是我的当前代码(非工作代码),我刚刚将
path
替换为
path。在相关位置替换(“/”,“\\”

import sys
import ftplib
import os
from ftplib import FTP
ftp=FTP("ftpserver.com")    
ftp.login('user', 'pass')

def downloadFiles(path,destination):
#path & destination are str of the form "/dir/folder/something/"
#path should be the abs path to the root FOLDER of the file tree to download
  try:
      ftp.cwd(path)
      #clone path to destination
      os.chdir(destination)
      print destination[0:len(destination)-1]+path.replace("/", "\\")
      os.mkdir(destination[0:len(destination)-1]+path.replace("/", "\\"))
      print destination[0:len(destination)-1]+path.replace("/", "\\")+" built"
  except OSError:
      #folder already exists at destination
      pass
  except ftplib.error_perm:
      #invalid entry (ensure input form: "/dir/folder/something/")
      print "error: could not change to "+path
      sys.exit("ending session")

  #list children:
  filelist=ftp.nlst()

  for file in filelist:
      try:
          #this will check if file is folder:
          ftp.cwd(path+file+"/")
          #if so, explore it:
          downloadFiles(path+file+"/",destination)
      except ftplib.error_perm:
          #not a folder with accessible content
          #download & return
          os.chdir(destination[0:len(destination)-1]+path.replace("/", "\\"))
          #possibly need a permission exception catch:
          ftp.retrbinary("RETR "+file, open(os.path.join(destination,file),"wb").write)
          print file + " downloaded"
  return

downloadFiles("/x/test/download/this/",os.path.dirname(os.path.abspath(__file__))+"\\")
输出:

Traceback (most recent call last):
  File "ftpdownload2.py", line 44, in <module>
    downloadFiles("/x/test/download/this/",os.path.dirname(os.path.abspath(__file__))+"\\")
  File "ftpdownload2.py", line 38, in downloadFiles
    os.chdir(destination[0:len(destination)-1]+path.replace("/", "\\"))
WindowsError: [Error 3] The system cannot find the path specified: 'C:\\
Users\\Me\\Desktop\\py_destination_folder\\x\\test\\download\\this\\'
回溯(最近一次呼叫最后一次):
文件“ftpdownload2.py”,第44行,在
下载文件(“/x/test/download/this/”,os.path.dirname(os.path.abspath(\uu file\uuuu))+“\\”)
下载文件中第38行的文件“ftpdownload2.py”
os.chdir(目标[0:len(目标)-1]+path.replace(“/”,“\\”)
WindowsError:[错误3]系统找不到指定的路径:“C:\\
用户\\Me\\Desktop\\py\u目标文件夹\\x\\test\\download\\this\'

有人能帮我把代码写下来吗?谢谢。

目录创建似乎与此问题类似,除了由于Windows格式的更改之外,此问题已被提出

因此,我建议在这些问题的公认答案中加入mkdir_p函数,然后看看windows是否会创建适当的路径

os.mkdir(destination[0:len(destination)-1]+path.replace("/", "\\"))
然后变成

newpath = destination[0:len(destination)-1]+path.replace("/", "\\")
mkdir_p(newpath)
这使用os.makedirs(path)方法来获取完整路径。您也可以将os.mkdir()替换为os.makedirs()


请注意,如果在许多地方使用替换路径,则只需继续使用newpath变量即可。在代码的其余部分中,这可能会更简单。

作者的这段代码真的有意义吗?我认为问题在于目录的创建。而不是创建
x
,然后
测试
,然后
下载
。。。它只是创建了
/x/test/download/this/
,这是错误的,对吗?