我真的需要系统调用中的列表,例如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 '~/'.
显然,如果有人能给我解决这个问题,我将非常感谢。
谢谢。
您可以在输入中处理~
。和 .
, ..
。
os.listdir(os.path.abspath(os.path.expanduser(user_input)))
参考:https://docs.python.org/2/library/os.path.html#os.path.path.path.expanduser
https://docs.python.org/2/library/os.path.html#os.path.abspath