Find files in google colab - path

The following snippet in my google colab:
import sys
import os
from google.colab import drive
drive.mount('/content/gdrive')
path = '/content/gdrive/MyDrive/MBA/MBA_USP/03_Modulo_Solucoes/16_Metologia_Pesquisa_Projeto_Conclusao/TCC'
fnames = os.listdir(path)
prints my files and directories:
['TCC', 'degree_dist.eps', 'mind_dataset']
Now I'd like to search for files starting from the above path, like so:
if os.path.exists('mind_dataset/file.csv'):
try:
# do something
But this is not finding file.csv, which is there. How do I append or join paths to my present path in colab, in order to find any file?

Related

Path Problem In Vs Code for Almost every thing including Files in CWD or imported library

Whenever I need to use a file in Vscode whether if it is imported or in the CWD Vscode can not find it unless I enter the file path fully, is there anything that help to use files in current working directory. or when I import something like QIcon I have the same problem.
from PyQt6.QtWidgets import QApplication, QWidget
from PyQt6.QtGui import QIcon
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle('PyQT6 Window')
self.setWindowIcon("qt.png") # this is not working I need to set a path

How to use this library in python "from digidevice import location"?

How can I get coordinates from user's device, using this library in python: "from digidevice import location".
I tried, but cant even import this library.
This should help: link
It seems like this will work:
from digidevice import location
loc = location.Location()
print(loc.position)

How do you load a file (.csv) into a Beeware/Briefcase application?

I am using kivy as the GUI and Briefcase as a packaging utility. My .kv file is in the appname/project/src/projectName/resources folder. I also need a .csv file, in the same folder, and want to use pandas with it. I have no problem with importing the packages (I added them to the .toml file). I can't use the full path because when I package the app, the path will be different on each computer. Using relative paths to the app.py file does not work, giving me a file not found error. Is there a way to read a file using a relative path (maybe the source parameter in the .toml file)?
kv = Builder.load_file('resources/builder.kv')
df = pd.read_csv('resources/chemdata.csv')
class ChemApp(App):
def build(self):
self.icon = 'resources/elemental.ico'
return kv
I just encountered and solved a similar problem with Briefcase, even though I was using BeeWare's Toga GUI.
In my case, the main Python file app.py had to access a database file resources/data.csv. In the constructor of the class where I create a main window in app.py, I added the following lines (The import line wasn't there, but included here for clarification):
from pathlib import Path
self.resources_folder = Path(__file__).joinpath("../resources").resolve()
self.db_filepath = self.resources_folder.joinpath("data.csv")
Then I used self.db_filepath to successfully open the CSV file on my phone.
__file__ returns the path to the current file on whatever platform or device.

How to read google spreadsheet using google colab

We can list out spreadsheet present in google drive using below command
from google.colab import drive
drive.mount('/content/drive')
!ls -l /content/drive/'Shared drives'
but unable to read spreadsheet using below command
import gspread
from oauth2client.client import GoogleCredentials
gc = gspread.authorize(GoogleCredentials.get_application_default())
gc.open('/content/drive/'Shared drives/data.gsheet').data available
and also one more problem we have space in sheetname(data available) and we don't have access to change sheetname
I have refer link:: https://colab.research.google.com/notebooks/io.ipynb
Kindly help on it.
You can use gc.open_by_url('gsheets_url') to open the document (no need to mount the drive). For the sheet name, you can use gsheets.worksheet('sheet name').
So on your case it'd go something like:
from google.colab import auth
auth.authenticate_user()
import gspread
from oauth2client.client import GoogleCredentials
# setup
gc = gspread.authorize(GoogleCredentials.get_application_default())
# read data and put it in a dataframe
gsheets = gc.open_by_url('your-link')
sheets = gsheets.worksheet('data available').get_all_values()
df = pd.DataFrame(sheets[1:], columns=sheets[0])
(credits to this post)
Update to the answer by Murilo Cunha, as it gives errors for authentication
from google.colab import auth
auth.authenticate_user()
import gspread
from google.auth import default
creds, _ = default()
gc = gspread.authorize(creds)
import pandas as pd
# read data and put it in a dataframe
gsheets = gc.open_by_url('Your link')
sheets = gsheets.worksheet('data available').get_all_values()
df = pd.DataFrame(sheets[1:], columns=sheets[0])
It's helpful if you could say what is the exact error you get when you run this script. However, I notice there are redundant quotes in following script,
gc.open('/content/drive/'Shared drives/data.gsheet')
replace extra above by following,
gc.open('/content/drive/Shared drives/data.gsheet')
Another option is to try gc.open_by_url() instead of gc.open()

How to import custom modules in google colab? (Solution given here not working)

I'm trying to import a custom module called 'clusterer.py' into my colab notebook. But the import function is not able to find the file. Is there any other way to import custom modules?
After mounting the drive, I have already tried this approach: How to import custom modules in google colab? with the result: 'No module named 'clusterer''
!ls /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/*.py
!cat /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/clusterer.py
import sys
sys.path.append('/content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection')
import clusterer
The output is as follows:
'/content/gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection/clusterer.py'
'/content/gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection/feature_extractor.py'
[Contents of the module 'clusterer.py']
ModuleNotFoundError: No module named 'clusterer'
Ok, I think I figured out one solution. In my case, I had to physically go inside the directory where the files were. This is what I did
1a. Restart the kernel but 'resetting all runtimes', specially if you've just added your file.py to the directory.
1b.cd gdrive/My Drive/Colab Notebooks/Omdena_Mars_Anomaly_Detection
!ls /content/gdrive/My\ Drive/Colab\Notebooks/Omdena_Mars_Anomaly_Detection/*.py
!cat /content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection/mylib.py
import sys
sys.path.append('/content/gdrive/My\ Drive/Colab\ Notebooks/Omdena_Mars_Anomaly_Detection')
5.import clusterer
Worked for me.
Thanks

Resources