将文件从一个文件夹复制到另一个

时间:2014-06-07 14:08:00

标签: python

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

    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')

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

1 个答案:

答案 0 :(得分:0)

从Python标准库要获取完整路径(以top开头)到dirpath中的文件或目录,请执行os.path.join(dirpath,name),这样就应该写 p>

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

拥有正确的道路。

相关问题