0

I am currently looking into working from different PCs on the same ownCloud data, doing file imports such as:

```data = pd.read_csv(r"C:\Users\User\ownCloud\Sample Information\folder1\folder2\data.csv")```

However, only "/Sample Information/folder1/folder2/data.csv" is independent of the PC that I use. The Jupyter notebook would be somewhere like this: r"C:\Users\User\ownCloud\Simulations\folder1\folder2\data_model.ipynb" I tried stuff like the following, but it wouldn't work:

```data = pd.read_csv(".../Sample Information/folder1/folder2/data.csv")```

Is there a concise way of doing these imports or do I need to use the os module and some combination of os.path.dirname( *... repeat for amount of subfolder until ownCloud is reached...* os.path.dirname(os.getcwd)) ?

Thanks for your help!

EDIT:

After some pointers I now use either of these solutions with v1 similar to this and v2 similar to this:

import os

ipynb_path = os.getcwd()
n_th_folder = 4

### Version 1 
split = ipynb_path.split(os.sep)[:-n_th_folder]
ownCloud_path = (os.sep).join(split)

### Version 2
ownCloud_path = ipynb_path
for _ in range(n_th_folder):
    ownCloud_path = os.path.dirname(ownCloud_path)
  • I don't think so there is a shortcut to do that without any imports. If you want me to tell you how to do that using os module, let me know here. – Rishabh Kumar Feb 09 '21 at 12:09

1 Answers1

0

You could replace username in the path with its value by string interpolation.

import os

data = pd.read_csv(r"C:\Users\{}\ownCloud\Sample Information\folder1\folder2\data.csv".format(os.getlogin())
manu190466
  • 1,557
  • 1
  • 9
  • 17
  • Hey, that idea is great and would fix my issue for some PCs. Unfortunately I also intend to use a linux system and the "ownCloud" folder has other names sometimes, eg "ownCloud_work". So I think everything before ```\Sample Information\...``` should be flexible – user15175446 Feb 09 '21 at 12:09
  • If it's flexible, where would you get this piece of information ? Do you want the users to set an environment variable ? – manu190466 Feb 09 '21 at 12:16
  • The way I thought about it so far is that I have my .pynb notebook which is n (quantity set by user in the script) folders inside the owncloud folder. So we would need to apply n-dots to the current work directory, or as it currently seems best to me, apply ```os.path.dirname``` n times. – user15175446 Feb 09 '21 at 12:27