代码之家  ›  专栏  ›  技术社区  ›  Carlos P Ceballos

Python-如何解决操作系统错误:[错误2]没有这样的文件或目录?

  •  1
  • Carlos P Ceballos  · 技术社区  · 7 年前

    我正在运行此命令:

    AWS_PROFILE=sandbox aws s3 cp local_path bucket --recursive
    

    这很好用。我想运行python脚本。这是我的代码:

    cmd = ['AWS_PROFILE=sandbox', 'aws', 's3', 'cp', local_path, bucket, '--recursive']
    subprocess.Popen(cmd).communicate()
    

    但它不起作用。

    错误回溯:

    File "bin/run_report.py", line 128, in main
        subprocess.Popen(cmd).communicate()
      File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 390, in __init__
        errread, errwrite)
      File "/usr/local/Cellar/python/2.7.14/Frameworks/Python.framework/Versions/2.7/lib/python2.7/subprocess.py", line 1025, in _execute_child
        raise child_exception
    OSError: [Errno 2] No such file or directory
    

    我不明白我做错了什么。我已经三次检查了到本地和到bucket的路径,如前所述,它是从shell运行的(我不想运行shell=True)

    1 回复  |  直到 7 年前
        1
  •  1
  •   SomeGuyOnAComputer Joril    7 年前

    使用 aws cli --profile 切换以选择配置文件。

    cmd = ['aws', '--profile=sandbox', 's3', 'cp', '--recursive', local_path, bucket]
    print(' '.join(cmd))
    subprocess.Popen(cmd).communicate()
    

    命令现在应为:

    aws --profile=sandbox s3 cp --recursive local_path bucket