0

I really need a list out of a system call such as os.system('ls ' + input) I've tried googling of course, but didn't find much.

Also tried different ways of coding it but I cannot get it to work.

import os

user_input = input("Specify directory: ")
directory = os.system('ls ' + str(user_input))
------------------------------------------------------------------
# I need a list made out of the directory variable.
# Also note that subprocessing doesn't work that well as it doesn't accept # bash special chars like '~/', whereas os.system() accepts that.
# What os.system(), subprocess.call() return is just a normal output and I # cannot get to list that.
# Also I have gotten my program to work with os.listdir() but os.listdir() # doesn't accept special chars like '~/'.

Obviously I would greatly appreciate it if somebody could give me a hand with this problem.

Thanks.

edvin
  • 178
  • 1
  • 7

1 Answers1

1

You can handle ~ in the input. And ., .. too.

os.listdir(os.path.abspath(os.path.expanduser(user_input)))

Ref: https://docs.python.org/2/library/os.path.html#os.path.expanduser

https://docs.python.org/2/library/os.path.html#os.path.abspath

rdas
  • 18,048
  • 6
  • 31
  • 42