Python 一行打印当前目录中的每个文件

标签 python cygwin cmd pipe

如何让下面的一行通过 Python 打印每个文件?

python -c "import sys;print '>>',sys.argv[1:]" | dir *.*

特别想知道如何通过管道输入 python -c。 接受 DOS 或 Cygwin 响应。

最佳答案

python -c "import os; print os.listdir('.')"

如果您想像问题中那样应用一些格式,

python -c "import os; print '\n'.join(['>>%s' % x for x in os.listdir('.')])"

如果你想使用管道,使用xargs:

ls | xargs python -c "import sys; print '>>', sys.argv[1:]"

或反引号:

python -c "import sys; print '>>', sys.argv[1:]" `ls`

关于Python 一行打印当前目录中的每个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/613007/

相关文章:

python - 在 Pandas 中,如何用最接近的非纳米值替换零值?

python - 当我有小数和大数时,我应该如何处理小数精度?

python - 具有不同输入的集成模型

c++ - Cygwin GDB 在尝试启动程序时出现错误 193

java - 无法使用输出流发送命令

lua - 如何运行 .lua 脚本?

python - FB edge.create 事件捕获时的多个请求

bash - cp : cannot stat `/vol/examples/tutorial/science.txt' : No such file or directory

c++ - mkdir 和 ofstream 的问题

jenkins - 尝试使用 -secret 选项通过 JNLP 通过命令行启动 Jenkins 从站时出现 401 Unauthorized 错误