python - 我需要一个由调用组成的列表,例如 os.system('ls' + path)
问题描述
我真的需要一个系统调用列表,例如 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.expanduser
https://docs.python.org/2/library/os.path.html#os.path.abspath
推荐阅读
- audio - Flutter (Dart):从麦克风获取/录制音频流并立即播放(实时)
- excel - 根据搜索条件创建行名列表
- symfony - Symfony 冒充另一个用户后获取原始用户
- node.js - express DELETE 路由行为怪怪的?意外的令牌“n”
- typescript - 如果路由发生任何变化,则将组件的路由更改为特定组件
- apache-nifi - Apache Nifi 1.7 替换文本处理器以进行时间戳操作
- scala - 如何使用 s & $ 访问 Pyspark 中的变量,就像在 Scala 中一样
- java - Java:以编程方式区分热敏打印机和经典打印机
- android - 浮动操作按钮在 Lollipop 中不起作用
- html - 引导轮播在移动设备上更改配置