-1

Share python script which uses gsutil to download a file from google cloud storage bucket. I am facing an issue while using gsutil Python scripts throws

Syntax error : Invalid Syntax

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • 1
    Please do not include images of text. Copy and paste the text in your answer, formatting it accordingly. – chb Apr 06 '18 at 00:02

1 Answers1

2

That syntax error occurs because that command is supposed to be run from the command line and not the Python interpreter itself.

If you search for cmd or PowerShell and try it in that shell instead, it should work - I am guessing you are on Windows from the direction of slashes, but it will be called terminal on Linux and Mac.

If you want to execute it from a Python script you could use subprocess or os.system().

With os.system():

os.system('gsutil cp gs:\\somepath C:\\Users\\sensekar\\Desktop\\Python\\')

With subprocess:

subprocess.Popen('gsutil cp gs:\\somepath C:\\Users\\sensekar\\Desktop\\Python\\', shell=True, stdout=subprocess.PIPE)

Note that using subprocess is better practice.

Remeber also to use \\ and not escape character \.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • Thanks. When i try to do that It throws an error message. C:\gsutil>C:\Python27\python.exe C:\Users\sensekar\Desktop\Python\GCSCopyfile.py There is no argcomplete library under the gsutil third-party directory (C:\Python27\lib\third_party). The gsutil command cannot work properly when installed this way. Please re-install gsutil per the installation instructions. – Senthil Sekar Apr 06 '18 at 15:49
  • @SenthilSekar The docs suggest `python` as a prefix to `gsutil`, does that fix the problem? So `python gsutil cp gs:\\somepath C:\\Users\\sensekar\\Desktop\\Python` Source: https://cloud.google.com/storage/docs/quickstart-gsutil – Xantium Apr 06 '18 at 16:48