1

I use Jupyter Notebooks on daily basis as a data scientist. I tend to use the same folders for original data, output files, graphics and so on. I usually use it the following way:

import os
import pandas as pd

# paths
data_path = '/foo/bar/data'
output_path = '/foo/bar/output'

# load data
df = pd.read_csv(os.path.join(data_path,'data.csv'))

# do some things with the data
df.to_csv(os.path.join.(output_path, 'mydata.csv'))

However, the paths are always the same. Hence, I copy the same paths in almost every notebook I use. My idea is to store the paths as a txt- or csv-file and to load and read the paths somehow. Is that possible? And if so, how?

EDIT: I tried to read the paths in from a file line by line. That works. But I get thrown the error No file in directory /foo/bar/data afterwards. Copying the exact same path and using it as described above works.

Rachel
  • 1,937
  • 7
  • 31
  • 58
  • the same way you read your data? create a storage.csv, put your both filepats in it, read it with read_csv and use them ? – Patrick Artner Nov 22 '17 at 16:46
  • 1
    put your paths in seperate lines of the file and use this: https://stackoverflow.com/questions/3277503/how-do-i-read-a-file-line-by-line-into-a-list ? Just acceess the list and use the paths ... then you will copy the "load my file code" over on each laptop - thats much better,is is not? – Patrick Artner Nov 22 '17 at 16:47
  • 1
    Possible duplicate of [How do I read a file line-by-line into a list?](https://stackoverflow.com/questions/3277503/how-do-i-read-a-file-line-by-line-into-a-list) – Patrick Artner Nov 22 '17 at 16:48
  • I couldn't make it work jsut yet. Somehowe, when I use os.join afterwards, it somehow doesn't work... Error: File not found. When I use it as described in the question, I have no issues. – Rachel Nov 23 '17 at 06:38
  • 1
    Have a look at [why-doesnt-os-path-join-work-in-this-case](https://stackoverflow.com/questions/1945920/why-doesnt-os-path-join-work-in-this-case) will help you – Patrick Artner Nov 23 '17 at 06:54
  • Solved it! Thank you! Should I delete/close the question? – Rachel Nov 23 '17 at 11:03
  • Do you think others with similar problem will be helped by what you could write up on your solution - write the answer including your Code yourself and accept it as answer. You'll even get a badge for it :) – Patrick Artner Nov 23 '17 at 11:10
  • I voted to close it instead. Your comment makes more sense then my question :) – Rachel Nov 23 '17 at 11:12

0 Answers0