2014-06-07 251 views
0

我试图制作一个脚本,将除压缩文件以外的所有文件从源文件夹复制到另一个目标文件夹,并将源文件夹中的压缩文件解压缩到目的地,这是我到目前为止已经到达:将文件从一个文件夹复制到另一个文件夹

import os 
    import zipfile 
    import shutil 
    myPath = "Source dir" 
    for root, dirs, files in os.walk(myPath): 
     for file in files: 
      if file.endswith('.zip'): 
       fh = open(file, 'rb') 
       z = zipfile.ZipFile(fh) 
       for name in z.namelist(): 
        outpath = "destination dir"#Put the name of the   destination folder 
        z.extract(name, outpath) 
       fh.close() 
      else: 
       fileList = os.listdir('source dir') 
       for f in fileList: 
        shutil.copy2(f, 'destination directory') 

该代码显示没有错误,但没有输出。

回答

0

从Python标准库为了得到一个完整的路径(与顶部开始)在dirpath一个文件或目录,请os.path.join(dirpath,名)所以你应该写

fh = open(so.path.join(root,file)) 

有正确的路径。

+0

您建议的更改无效。您可以告诉我确切的错误。 – user3703286

+0

用我给你的表达式,路径仍然是相对于'源路径'。你可以尝试'os.path.join(os.path.join('source dir',root),file),用真实的源代码路径代替'source dir'。 –

相关问题