whytheq whytheq - 3 months ago 7
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
print(a)


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
f
instead of the hard-coded
X
but if I change data/X to a string 'data/' and concatenate f to the end of it then I does not return anything.




note

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

Answer

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
    print(a) 
Comments