尝试复制目录中的所有Excel文件,但没有任何反应,也没有调用错误

时间:2012-09-28 15:07:01

标签: python batch-file copy shutil

我正在编写一个python脚本,将目录树中的所有Excel文件复制到另一个目录。直截了当,对吧?

好吧,出于某种原因,shutil.copy(或者copy2或者copy copy)没有做任何事情,甚至没有吐出错误信息。有什么想法吗?

def goFunc(self, event):
    print "Starting Go"
    for (path, dirs, files) in os.walk(self.path):
        print path
        print dirs
        print files
        for every_file in files:
            filename = str(path) + str(every_file)
            print filename
            if filename.endswith('.xlsx'):
                print "Copying " + filename + " to " + str(self.path2)
                shutil.copyfile(filename, str(self.path2))
    print "All DONE!"

所以,除了复制步骤之外我添加了一个尝试,似乎这就是问题所在的步骤:

def goFunc(self, event):
    print "Starting Go"
    for (path, dirs, files) in os.walk(self.path):
        print path
        print dirs
        print files
        for every_file in files:
            filename = str(path) +'/' + str(every_file)
            print filename
            if filename.endswith('.xlsx'):
                print "Copying " + filename + " to " + str(self.path2)
                try:
                    shutil.copyfile(filename, str(self.path2))
                except:
                    print "Something went wrong"
                    pass
    print "All DONE!"

现在输出是:

Starting Go
/Users/laptop/Desktop
[u'test']
[u'.DS_Store', u'.localized', u'Maytag.xlsx', u'mer.xlsx']
/Users/laptop/Desktop/.DS_Store
/Users/laptop/Desktop/.localized
/Users/laptop/Desktop/Maytag.xlsx
Copying /Users/laptop/Desktop/Maytag.xlsx to /Users/laptop/test
Something went wrong
/Users/laptop/Desktop/mer.xlsx
Copying /Users/laptop/Desktop/mer.xlsx to /Users/laptop/test
Something went wrong
/Users/laptop/Desktop/test
[]
[]
All DONE!

由于某种原因,文件仍未被复制。

解决方案:

看起来我需要将文件名添加到目的地。现在它就像一个魅力!感谢大家的时间和帮助。

def goFunc(self, event):
    print "Starting Go"
    for (path, dirs, files) in os.walk(self.path):
        print path
        print dirs
        print files
        for every_file in files:
            filename = str(path) +'/' + str(every_file)
            print filename
            if filename.endswith('.xlsx'):
                print "Copying " + filename + " to " + str(self.path2) + '/' + str(every_file)
                try:
                    shutil.copyfile(filename, str(self.path2)+'/'+ str(every_file))
                except:
                    print "Something went wrong"
                    pass
print "All DONE!"

2 个答案:

答案 0 :(得分:1)

你可以(ab)使用shutil.copytree

import shutil
import os.path

src = '/home/jon/Development/number5'
dst = '/home/jon/tmpso'

def not_xlsx(path, names):
    return {name for name in names if os.path.isfile(name) and not name.endswith('.xlsx')}

shutil.copytree(src, dst, ignore=not_xlsx)

如果你想要更复杂的通配符/ etc ...匹配(可能是这样的话),你也可以查看fnmatch模块:

def not_xlsx(path, names):
    actual_files = filter(os.path.isfile, names)
    return set(actual_files).difference(fnmatch.filter(actual_files, '*.xlsx'))

答案 1 :(得分:0)

使用os.walk方法很好,但它也会返回[].之类的内容,所以要小心。 这是一个应该的类方法:

def goFunc(self, event):
    '''this should be enough to copy files that end with xlsx'''
    print "Starting Go"
    for file in os.listdir(Yourdir):
        if file.endswith('.xlsx'):
                print "Copying " + filename + " to " + str(self.path2)
                shutil.copyfile(filename, self.path2)
    print "All DONE!"

好的,不要重新发明轮子:

dirs=[o for o in os.listdir(TopOfTree) if os.path.isdir(o)]
for dir in dirs:
    goFunc(event)

Getting a list of all subdirectories in the current directory