Skip to content
Advertisement

Convert the output of ls in a python list [closed]

I get the listing of a directory using bash command ls(can’t use python on remote)

How can I convert the return of ls from remote in a python list/set containing filenames?

Advertisement

Answer

you can just use os.listdir, like this:

import os
files = os.listdir()

EDIT: if you have any control over the ls cmd, you can add the -1 flag, to get one filename per line, then use .splitlines() to turn it into a list.

regular split on the regular ls would fail on filenames that contain spaces.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement