In the modern era of the internet, a large number of mundane or work-related tasks are carried out over the internet. Compromise of data stored on a device could cause massive harm to an individual/company. Considering the rise in the number of malware, viruses, worms, trojan horse etc the security of data at rest has become a concern for many people.
One of the most common ways of protecting the data is by creating a backup of it. While backups do provide some protection to critical data, it should be situated far away from the original data. Such that even if the system containing the original data gets compromised somehow, the backup data would still be safe. Cloud storage is a technology built to fulfil that purpose.
Any person having a google account can use 15 Gigabytes of free cloud storage for storing their data. This solves the problem of an offsite backup. But uploading file data every time could be a little cumbersome. So to ease that process, we will create a python program that looks inside a directory and uploads any files inside it to our Google Drive account.
For this purpose, we will be using pydrive library. This module is not preloaded with python. So to install it execute the following command in the command-line:
pip install pydrive
Creating OAuth credential
For authenticating successfully to our google account every time we want to upload some data to it, we need to create an OAuth credential.
After completing the method above we would end up with a file of a name similar to client_secret_(really long ID).json. Rename the file to “client_secrets.json” and place it in the same directory where the main python program will be created.
Code
from pydrive.drive import GoogleDrive from pydrive.auth import GoogleAuth # For using listdir() import os # Below code does the authentication # part of the code gauth = GoogleAuth() # Creates local webserver and auto # handles authentication. gauth.LocalWebserverAuth() drive = GoogleDrive(gauth) # replace the value of this variable # with the absolute path of the directory path = r"C:\Games\Battlefield" # iterating thought all the files/folder # of the desired directory for x in os.listdir(path): f = drive.CreateFile({'title': x}) f.SetContentFile(os.path.join(path, x)) f.Upload() # Due to a known bug in pydrive if we # don't empty the variable used to # upload the files to Google Drive the # file stays open in memory and causes a # memory leak, therefore preventing its # deletion f = None |
The above code upon execution executes an instance of the default browser of your OS. It then asks for providing permissions to pydrive for accessing your Google Drive account. When all the desired permissions are granted, the browser shows a message
The authentication flow has completed.
After which the process of uploading files to Google Drive initiates.
Certain things to keep in mind while using the above program:-
- Make sure “client_secrets.json” is inside the same directory as the Python program
- The directory(path) should not contain any subdirectories inside it
- Your Google Drive should have sufficient empty space in order to incorporate for new files
- The new files will be created in the root of Google Drive storage
Recommended Posts:
- Get list of files and folders in Google Drive storage using Python
- Python | Uploading images in Django
- Download Anything to Google Drive using Google colab
- Ways to import CSV files in Google Colab
- Downloading files from web using Python
- Rename multiple files using Python
- Check if directory contains files using python
- Compare two files using Hasing in Python
- Create temporary files and directories using Python-tempfile
- How to Delete files in Python using send2trash module?
- Joining Excel Data from Multiple files using Python Pandas
- Working with wav files in Python using Pydub
- Python program to find files having a particular extension using RegEx
- Deleting Files in HDFS using Python Snakebite
- Creating Files in HDFS using Python Snakebite
- Python | Get a google map image of specified location using Google Static Maps API
- Creating a dataframe using Excel files
- Working with excel files using Pandas
- Creating a dataframe using CSV files
- Working with csv files in Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

