Martyn
Martyn

Reputation: 61

!ls in Jupyter notebook (Python 3)

If I use:

!ls '/Users/martyn/Documents/rawData'

it gives me a list of the files in the required directory.

But I want to paramterize this. I tried:

pathData = '/Users/martyn/Documents/rawData'
!ls pathData

But this gives the error:

ls: pathData: No such file or directory

I can see the problem ... but can't see how to fix it. Any help would be greatly appreciated.

Upvotes: 5

Views: 19560

Answers (5)

Imran
Imran

Reputation: 885

You can list the files and folder in any directory using folling command

%ls pathData

for current directory use

%ls

if you want to go one folder back you can use

%ls '../'

For two folders back

%ls '../../'

Upvotes: 2

Idris kzl
Idris kzl

Reputation: 11

Maybe you can also use:

from glob import glob

#path_list=glob("put your file path here.extension")
path_list=glob("C:/Users/..../PYTHON/module1/data.jpg")

Upvotes: 1

Nic Scozzaro
Nic Scozzaro

Reputation: 7373

You can also use:

import subprocess
print(subprocess.check_output(['ls', '-lhS']).decode('utf-8'))

Upvotes: 0

Marjan Radfar
Marjan Radfar

Reputation: 329

Using only %ls gives you the list in your current directory.

Upvotes: 1

Slam
Slam

Reputation: 8572

You probably need

!ls {pathData}

or

!ls $pathData

Upvotes: 9

Related Questions