To create a new text file in Google Drive with a given content through python script using Pydrive Module














































To create a new text file in Google Drive with a given content through python script using Pydrive Module



Steps :
  1. The all important code :
    1. # To create a text file in G drive with given content
    2. from pydrive.drive import GoogleDrive

    3. # Create GoogleDrive instance with authenticated GoogleAuth instance.
    4. drive = GoogleDrive(gauth)

    5. # Create GoogleDriveFile instance with title 'TaskManager.txt'.
    6. file = drive.CreateFile({'title':'TaskManager.txt', 'mimeType':'text/plain'})

    7. file.SetContentString(''India, officially the Republic of India, is a country in South Asia. It is the seventh-largest country by area, the second-most populous country, and the most populous democracy in the world.''')
    8. file.Upload() # Upload file.
  2. In line 2 we are importing GoogleDrive class from pydrive.drive library and then in line 5 we are creating an object of GoogleDrive class.
  3. Then in line 7 we are creating a GoogleDriveFile instance with the title 'TaskManager.txt' and in line   we are calling the SetContentString method using this 'file' instance and are also passing the content for the newly created file as a string input.
  4. Lastly, we are uploading this newly created file onto the drive using the Upload method of GoogleDrive class.



If you wish to see the output instance after running the above code :


Comments