gsutil 遇到 subprocess.Popen 的语法错误
gsutil got an syntax error with subprocess.Popen
在我的案例中,我想将所有 pdf 文件上传到 google 云存储。
所以我使用 google-sdk 和 pytho 子进程来练习而不是“google.cloud.storage API”。
但是下面有错误:
代码:
from subprocess import Popen
def subprocess_cmd(command):
print(f'$>: {command}')
process = Popen(command,
stdout=subprocess.PIPE,
executable='/bin/bash',
shell=True)
proc_stdout = process.communicate()[0].strip()
print(proc_stdout.decode("utf-8"))
执行函数:
command = "gsutil -m cp -r ./source/*(.pdf|.PDF) gs://<bucket_name>"
subprocess_cmd(command)
错误:
/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `gsutil -m cp -r ./source/*(.pdf|.PDF) gs://<bucket_name>'
您应该改用这一行:
command = "gsutil -m cp -r ./source/{*.pdf,*.PDF} gs://<bucket_name>"
请参阅 this document about bash
wildcards. You can also see this document 中关于 gsutil
通配符的 { } (curly brackets)
部分。
在我的案例中,我想将所有 pdf 文件上传到 google 云存储。
所以我使用 google-sdk 和 pytho 子进程来练习而不是“google.cloud.storage API”。
但是下面有错误:
代码:
from subprocess import Popen
def subprocess_cmd(command):
print(f'$>: {command}')
process = Popen(command,
stdout=subprocess.PIPE,
executable='/bin/bash',
shell=True)
proc_stdout = process.communicate()[0].strip()
print(proc_stdout.decode("utf-8"))
执行函数:
command = "gsutil -m cp -r ./source/*(.pdf|.PDF) gs://<bucket_name>"
subprocess_cmd(command)
错误:
/bin/bash: -c: line 0: syntax error near unexpected token `('
/bin/bash: -c: line 0: `gsutil -m cp -r ./source/*(.pdf|.PDF) gs://<bucket_name>'
您应该改用这一行:
command = "gsutil -m cp -r ./source/{*.pdf,*.PDF} gs://<bucket_name>"
请参阅 this document about bash
wildcards. You can also see this document 中关于 gsutil
通配符的 { } (curly brackets)
部分。