PyDrive
PyDrive copied to clipboard
Cannot get list of files inside a folder
drive.ListFile({'q': "'%s' in parents and trashed=false" % folder_id}).GetList()
- returns 500
Interesting. Can you provide some more information? Is there any more info in the error, are you certain that the folder_id exists? Could you try whether either of the following work (e.g. does not return an error):
drive.ListFile({'q': 'trashed=false', 'maxResults': 10}).GetList()
drive.ListFile({'q': "'%s' in parents" % folder_id}).GetList()
@RNabel
This script checks for a file in a google drive folder and updates it if it exists. This has been working well for a long time except for the last 2 weeks. It worked a very few times when I reran the script.
I have another line in the script to read all folders in the root and that works. -> drive.ListFile({'q': "'root' in parents and trashed=false"}).GetList()
But searching inside a folder keeps returning 500. I also tried the two lines you replied above, first line works but the second does not.
Cool, makes sense. You can test API calls in the Drive API docs - you may want to check that it works in there: https://developers.google.com/drive/v2/reference/files/list#try-it
(In the right side window titled "Try this API") Ignore all the fields except for the q
field in which you put the '<file id>' in parents
string and hit execute. You will likely be asked to confirm your account. If you have multiple accounts make sure you select one that has access to the Drive folder whose ID you are testing.
This will help locate where the problem is. I just checked the above with my account and it worked. If this works, my guess would be that there is something funky going on with your PyDrive setup.
Hello @RNabel I tried the API explorer first by specifying the actual file id. It worked but if i try 'folder id in parents' it returns 500.
{
"error": {
"errors": [
{
"domain": "global",
"reason": "internalError",
"message": "Internal Error"
}
],
"code": 500,
"message": "Internal Error"
}
}
Cool, that narrows it down to the Python code or auth setup. You need to make sure that you put the id
in single quotes inside the double quotes. Could you double-check that you placed the quotes correctly (e.g. 'q': "'folder id' in parents"
as in my first answer)?
I have the same issue, but with a 400 error for "code".
The exact same Pydrive code worked a few months ago. Now it seems to have broken.
Same here @kurt-rhee
Hello @vathsanvk @kurt-rhee @mitchhhh
I got to this page because in the beginning I got a similar issue, not being able to get ListFile() results in certain folders.
I tried @RNabel suggestion and used the Gdrive API test page.
I used this string in the q inputbox: '1wlAyrKSlcswNG_SRjjPvehexk-1QWJML' in parents
And then I got a file list, as expected.
But the same request using PyDrive returns me an empty list instead:
This was my PyDrive request:
drive = GoogleDrive(gauth)
print(" ----- SEARCH FILES IN A GIVEN FOLDER ----- ")
file_list = drive.ListFile({'q': "'1wlAyrKSlcswNG_SRjjPvehexk-1QWJML' in parents"}).GetList()
print(file_list)
print(" ----- END OF FILE_LIST ----- ")
And here is the output:
----- SEARCH FILES IN A GIVEN FOLDER -----
[]
----- END OF FILE_LIST -----
That is a public Gdrive folder I created, so feel free to test it with PyDrive and compare your results.
After that initial PyDrive FileList() fail, I did a bit more tests lead me to some other conclusions:
- First of all, I thought the problem was PyDrive not being able to access my Gdrive folders: it had been able to create, upload, list and download text files in my Gdrive root (I started following this example). But later on FileList() was not working anywhere else for me, other than that Gdrive root folder.
- But then I started to move and rename files (using Gdrive web interface), and I realized all those files not being detected by PyDrive FileList() in my folders ... they had been uploaded manually by me (using Google Drive webpage).
Whereas all the files which were being detected by PyDrive FileList(), they had all been previously created and uploaded by PyDrive itself to my Gdrive root (using the example in my first line). - I confirmed this when I later moved one of those files to some other folder, then PyDrive ListFile() was able to find and list it.
Conclusion: PyDrive is only being able to list content created by itself ??? Could someone else try to replicate this? I think @kdschlosser had a similar problem in #122
Is this the intended behaviour? Perhaps some security switch to avoid PyDrive destroying stuff not created by itself?
But, in that case, is it posible to change this behaviour and let PyDrive manage the whole Gdrive account?
Thanks a lot for your help
There has to be some kind of a marker we are missing that is only allowing files/folders created by pyDrive to be seen by PyDrive. It has been a while since I have played with this. But I do know I searched and searched the internet and I was not able to locate any information as to why I am unable to see file/folders created in the web interface. the permissions are the same. the metadata is the same.. it's beyond me. I am thinking that the only way to get a solid answer with this is we are going to have to ask google.
and to be honest. after a year and a half. i am not getting my hopes up of this problem being fixed. or #122 either.
Well ... my problem was gone after I found #108 (by default, github does not show closed issues, so I had completely missed it).
I posted my solution in StackOverflow (where I originally came from and discovered PyDrive) and in #122 as it seems closer to what @kdschlosser related there.
I'd just note that, from the above, it seems that if this isn't working for anyone it's likely a permissions issue as @abubelinha notes. Otherwise I'd affirm that the syntax @abubelinha used worked for me, and more so did so when combined with a trashed=False filter. Actual syntax for it can be seen in practice at https://github.com/gsuitedevs/PyDrive/issues/117#issuecomment-467457774.
I have not done any real digging into the cause of the problem. I do not know if the problem is with the google API. or if it is with PyDrive, I would have to add some data logging code to see what the returned data is from the request. this is a really bad issue in my book. it severely handicaps the program. I do remember checking all of the permissions. and they checked out OK. I find it strange that pyDrive cannot see a folder if it is made using the UI but if the folder is made using pyDrive then it is fine. I am very suspect of it being pyDrive and there being a mime type that is missed, But there is no real clear API documentation from google to nail it down 100% without having to go all old school and print out the data.
I think the issue must be something different; the folder I was accessing in the above code was made using the UI and accessing it worked just fine in my case. Are your permissions right? And is your folder ID correct?