whytheq whytheq - 1 year ago 72
Python Question

Loop needs to reference list returned by terminal in notebook

I have a folder structure with 3 folder X,Y and Z. Each of these folders contains 3 files X1.csv, X2.csv, X3.csv, Y1.csv, Y2.csv, Y3.csv, Z1.csv, Z2.csv, Z3.csv.

In my jupyter notebook I can do the following:

folders = ['X','Y','Z']
for f in folders:
a = !ls data/X

This retruns the following:

['X1.csv', 'X2.csv', 'X3.csv']
['X1.csv', 'X2.csv', 'X3.csv']
['X1.csv', 'X2.csv', 'X3.csv']

I want to change
!ls data/X
so that it uses
instead of the hard-coded
but if I change data/X to a string 'data/' and concatenate f to the end of it then I does not return anything.


I could import os and then use listdir but I'm wondering if the above is possible.

Answer Source

Simply do not use magical notebook commands to do something that can be easily done with pure python. Magic notebook commands are supposed to be used for quick hacks and supporting non-python functionality, not to be actually placed as a part of your code.

However, if for odd reason you really have to, you need to reference your variables with bash like-way ($variable instaed of variable)

folders = ['X','Y','Z']  
for f in folders:
    a = !ls data/$f
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download