Search code examples
pythondirectorydashboardholoviews

Python: Navigate directories using forwardslash and backslash


I have written some code in Jupyter Notebook, that runs locally on my windows PC. When it imports folders i use "\".

However i have just moved all the folders to my google drive, & opened it with Colab. Now the in the directory path the folders are separated with "/", thus giving error.

How do I import foders regardless of whether i am running it locally on my PC, or online.

# added this bit so i can import and run the code in colab 
import os
from google.colab import drive
drive.mount('/content/drive')
os.chdir('/content/drive/My Drive/Dashboarding_Project/Folder_with_all_IV_curves/Python_IV')

#From the JUPYTER NOTEBOOK file 

import pandas as pd
import os
import re


 #these variables contain hte excell file with hte mask info & the file with the batch info 
meta_mask_name = "MASK-1_2020-03-01.xlsx"
meta_batch_name = "BATCH-1-2_2020-8-20--EXAMPLE.xlsx"


 #This is the main directory of the foler structure 
path_parent = os.path.dirname(os.getcwd())

print("Main folder: ", path_parent)
print("Folders: ",  os.listdir(path_parent))
print (os.listdir(path_parent +r"\MASK_META")) # this gives error now that i am using it online. 


OUT:


FileNotFoundError: [Errno 2] No such file or directory: '/content/drive/My Drive/Dashboarding_Project/Folder_with_all_IV_curves\\MASK_META'

CONTEXT

I am using both Colab and Jupiter becuase:

  • Colab is easy to share, dont have to download anything and can access it anywhere.
  • Jupyter i can use and Deploy locally PANEL, and Colab wont allow me to view it locally.

My end objective is to have:

  • A dashboard (Panel or something else) fully online
  • Dashboard runs on a server not my PC (Like Heroku)
  • I can send the link to someone and they can view it.

Maybe someone has a solution to this problem that avoids the main quesiton.


Solution

  • Instead of os module, one can use pathlib module, available from Python 3.4.

    pathlib module provides an API for filesystem operations. pathlib.Path class is a portable representation for filesystem paths for all supported platforms:

    from pathlib import Path
    
    # Print the user's home directory
    print(Path.home())
    # Windows example output:
    # WindowsPath('C:/Users/username')
    # Linux example output:
    # PosixPath('/home/username')
    

    pathlib.Path works with forward slash path separators on all platforms. Below is a Windows example:

    Path.home().joinpath('../').resolve()
    # WindowsPath('C:/Users')
    

    However backslash will not work on all platforms as expected:

    Path.home().joinpath('..\\').resolve()  # Note double backslash is required for backslash escaping
    # On Windows:
    # WindowsPath('C:/Users')
    # On Linux:
    # PosixPath('/home/username/..\\')
    

    My recommendation is to use Posix-like paths (forward slash separators) on all platforms with pathlib.Path providing the mapping of path separators.


    Rewriting the code in the question to use pathlib:

    from pathlib import Path
    
    
    path_parent = Path.cwd().parent
    
    
    def dir_contents_as_string(directory):
        # Explicit conversion of Path to str is required by str.join()
        return ", ".join(str(path) for path in directory.iterdir())
    
    
    print("Main folder: ", str(path_parent))
    print("Main folder contents: ",  dir_contents_as_string(path_parent))
    print(dir_contents_as_string(path_parent.joinpath("MASK_META")))