Python Forum
string.format() suddenly causing errors with google drive API - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: string.format() suddenly causing errors with google drive API (/thread-34123.html)



string.format() suddenly causing errors with google drive API - zwitrader - Jun-28-2021

This morning I abruptly started having problems with previously working (unchanged) code to access google drive via their API.

Briefly, the following code was working this morning (and for the past several days), but suddenly stopped working today:

myFolderID = 'kjl234987fh435kjwef920fsdlksA6GD'
myFileList = service.files().list(q=" '{0}' in parents and trashed=false".format(myFolderID) , fields='files(id, name)'.execute()
Conversely, if I include the string directly in the query, I get no error and am still able to run the code:

myFileList = service.files().list(q=" 'kjl234987fh435kjwef920fsdlksA6GD' in parents and trashed=false".format(myFolderID) , fields='files(id, name)'.execute()
So, the problem seems to be related to the way string.format() is interacting with the query.

I confirmed that the variable
myFolderID
was storing the correct value by issuing various print() commands:

print("{0}".format(myFolderID))
print(f"{myFolderID}")
print("%s" % (myFolderID))
all of which printed the correct string (kjl234987fh435kjwef920fsdlksA6GD). However, all of these string formatting variations resulted in the same error when used in the query.

I am running Python 3.9 on macOS Catalina. Here is the Traceback error displayed in Terminal:

Error:
Traceback (most recent call last): File "/Users/user/Desktop/Script_B.py", line 508, in <module> myFileList = service.files().list(q=" '{0}' in parents and trashed=false".format(myFolderID) , fields='files(id, name)'.execute() File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper return wrapped(*args, **kwargs) File "/Users/user/opt/anaconda3/lib/python3.8/site-packages/googleapiclient/http.py", line 935, in execute raise HttpError(resp, content, uri=self.uri) googleapiclient.errors.HttpError: <HttpError 404 when requesting https://www.googleapis.com/drive/v3/files?q=parents+%3D+%27kjl234987fh435kjwef920fsdlksA6GD%0A%27+and+trashed%3Dfalse&fields=files%28id%2C+name%29&spaces=drive&alt=json returned "File not found: .". Details: "[{'domain': 'global', 'reason': 'notFound', 'message': 'File not found: .', 'locationType': 'parameter', 'location': 'fileId'}]">