1

我正在尝试从内核文件夹复制 *.patch 文件并遇到以下错误

 Traceback (most recent call last):
  File "copytest.py", line 47, in <module>
    main()
  File "copytest.py", line 44, in main
    copystuff(src,dest)
  File "copytest.py", line 18, in copystuff
    shutil.copytree(src, dest)
  File "C:\CRMApps\apps\Python262\lib\shutil.py", line 140, in copytree
    names = os.listdir(src)
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: '\\\\buildloc\\kernel\\*.patch/*.*' 

代码:-

import argparse
import shutil, errno
import os
def copystuff(src, dest):
    #print "IN COPY STUFF"
    if os.path.isfile(src):
        dest_dirname = os.path.dirname(dest)
        #print "DEST DIR NAME" + dest_dirname
        if not os.path.isdir(dest_dirname):
           # print "MAKING DEST DIR"
            os.makedirs(dest_dirname)
        print src
        print dest
        shutil.copy2(src, dest)
    else:
        print src
        print dest
        shutil.copytree(src, dest)

def main ():
    copy_list=[r'\kernel\*.patch']
    #copy_list=['out\target\product\msm8226\obj\KERNEL_OBJ\vmlinux']
    parser = argparse.ArgumentParser(description='This is copy binaries for android build script)
    parser.add_argument('-v','--build_version',action='store',dest='build_version',help='<Required> Build version',required=True)
    parser.add_argument('-s','--source',action='store',dest='source',help='<Required> source path',required=True)
    parser.add_argument('-d','-destination',action='store',dest='destination',help='<Required>destination path',required=True)
    results = parser.parse_args()# collect cmd line args
    build_version = results.build_version
    print "BUILD_VERSION: " + build_version
    source = results.source
    print "SOURCE PATH: " + source
    destination = results.destination
    print "DESTINATION PATH: " + destination
    dest_fastboot=  destination + r'\out\target\product\msm8226'
    if not os.path.isdir(dest_fastboot):
        # print "MAKING DEST DIR"
        os.makedirs(dest_fastboot)
        shutil.copy2('Fastboot_load.bat', dest_fastboot)
    for item in copy_list:
        item = item.strip()
        print "ITEM: " + item
        src = source + item
        dest = destination + item
        copystuff(src,dest)

if __name__ == '__main__':
    main()
4

1 回答 1

4

您的问题很简单:\\buildloc\kernel\*.patch/*.*不是目录的名称。Windows 文件名不能包含星号以及其他禁止字符。shutil.copytree()需要目录的名称;它不做通配符。

如果您的意图是从名称以 结尾的目录中复制所有文件.patch,则一种方法是使用类似glob.glob标识您想要的目录,然后shutil.copytree()复制每个目录。另一个想法是提供一个回调函数copytree来过滤掉你不想要的目录。

于 2013-10-05T06:10:30.947 回答