我需要一个由os.system('ls'+ path)之类的调用组成的列表

时间:2019-05-01 08:09:33

标签: python python-3.x command

我真的需要系统调用清单,例如os.system('ls'+ input) 我当然尝试了谷歌搜索,但没有发现太多。

也尝试了不同的编码方式,但我无法使其正常工作。

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 '~/'.

很显然,如果有人可以帮我解决这个问题,我将不胜感激。

谢谢。

1 个答案:

答案 0 :(得分:1)

您可以在输入中处理~。还有...

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

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

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