Problems with command using * wildcard in subprocess

后端 未结 1 1269
春和景丽
春和景丽 2020-12-11 23:21

I\'m trying to copy files from one location to another using subprocess library and Popen method. When runing following script I\'m getting the error cp: cannot stat /

相关标签:
1条回答
  • 2020-12-12 00:10

    Expanding the * (globbing) is a function of your shell, bash for example. Therefore you'd have to use the keyword argument shell=True in your subprocess.Popen call.

    However, for this case I'd strongly suggest to use shutil.copytree instead.

    (First of all, because it's much simpler (see Zen of Python) and less error-prone. Dealing with errors is much cleaner, you get nice exceptions including a list of errors (for multi file operations like yours), and you don't have to deal with spawning a subprocess and communicating with it. Second, it's an unnecessary waste of resources to fork a child process if you don't need to. Other issues include quoting / escaping and possibly introducing security vulnerabilities into your code if you fail to properly sanitize user input.)

    For example:

    from shutil import copytree
    from shutil import Error
    
    try:
       copytree('dir_a', 'dir_b')
    except (Error, OSError), e:
        print "Attempt to copy failed: %s" % e
    

    Also, you shouldn't build filesystem paths by concatenating strings together, but instead use os.path.join(). That will use the correct directory separator (os.sep) for the current OS and allow you to easily write portable code.

    Example:

    >>> import os
    >>> os.path.join('/usr/lib', 'python2.7')
    '/usr/lib/python2.7'
    

    Note: os.path.join still only does (smart) string manipulation - it doesn't care if that path is accessible or even exists.

    0 讨论(0)
提交回复
热议问题