1

I'm trying to copy files from one folder to another using multiple source locations and wildcards as a part of filenames, but for some reason glob.glob is not working as I have expected (there are only single files per wildcard but version of the snapshot is changing) - getting an error need string or buffer, list found.

Part of the python code for that looks like:

content=[]
tomcatFiles=[]

def addToContent(srcFile, destFile):
    info={'src': srcFile, 'dest': destFile}
    content.append(info)

def addToTomcatFile(srcFile, destFile):
    info={'src': srcFile, 'dest': destFile}
    tomcatFiles.append(info)

def main():
    baseDir=sys.argv[1]
    intellijProjDir=sys.argv[2]

    deploy_dir=baseDir+'/TransferFiles'
    working_dir=intellijProjDir
    tomcatDir=deploy_dir+"/tomcat"

    addToTomcatFile('/project1/target/project1*.war', '/tomcat/project1.war')
    addToTomcatFile('/project2/target/project2*.war', '/tomcat/project2.war')
    addToTomcatFile('/projectX/target/projectX*.war', '/tomcat/projectX.war')

    for infoObj in tomcatFiles:
        addToContent(infoObj['src'], infoObj['dest'])

    for infoObj in content:
        shutil.copy2(glob.glob(working_dir + infoObj['src']), deploy_dir + infoObj['dest'])
        print('Copied ' + infoObj['dest'])
1
  • 1
    Please read minimal reproducible example. The minimal code to show this problem is literally one line: shutil.copy2(glob.glob('foo*'), 'bar'). Commented Apr 20, 2019 at 1:11

2 Answers 2

1

shutil.copy2 requires a string (or buffer) for its src argument, not a list. Since you're sure there will only ever be one match, get the first item returned by glob.glob, i.e. glob.glob(...)[0].

Sign up to request clarification or add additional context in comments.

Comments

1

Like this:

for f in glob.glob('foo*'):
    shutil.copy2(f, 'bar')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.