How do you successfully invoke gsutil rsync from a python script?

谁都会走 提交于 2021-02-08 08:10:50

问题


I am trying to execute the following line

gsutil -m rsync s3://input gs://output

in python. When running this line in the shell terminal it works fine. However, I am trying to run this in a python script by using the following line.

subprocess.Popen(["gsutil", "-m", "rsync", "s3://input", "gs://output"])

However it just hangs forever. It outputs the following:

Building synchronization state...
Starting synchronization...

The bash command successfully prints:

Building synchronization state...
Starting synchronization...
Copying s3://input/0000
[0/1 files][  1.0 MiB/ 5.1 MiB]   (number here)% Done

and the file shows in my gs bucket


回答1:


I'm guessing this is because the last two lines are probably written to stderr instead of stdout. Can you try using the call to Popen as a context manager and then calling communicate() to read from the output streams?

proc = subprocess.Popen(["gsutil", "-m", "rsync", "s3://input", "gs://output"])
try:
    outs, errs = proc.communicate(timeout=15)
    # now you can do something with the text in outs and errs
except TimeoutExpired:
    proc.kill()
    outs, errs = proc.communicate()


来源:https://stackoverflow.com/questions/52828000/how-do-you-successfully-invoke-gsutil-rsync-from-a-python-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!