我在python
尝试这样的:烦恼使用python脚本里面bash命令时
import os
process="find . -type f -name *.out*.xyz'"
print "the number of xyz files is %s" %os.system(process)
当我尝试从它的工作原理命令行,但是从蟒蛇返回数字0,当它是4。我认为使用os.system一定会有问题。任何帮助?
我在python
尝试这样的:烦恼使用python脚本里面bash命令时
import os
process="find . -type f -name *.out*.xyz'"
print "the number of xyz files is %s" %os.system(process)
当我尝试从它的工作原理命令行,但是从蟒蛇返回数字0,当它是4。我认为使用os.system一定会有问题。任何帮助?
是的,你得到的是返回代码“0”,意思是“成功完成,没有错误”,而不是标准输出文本输出“4”。
你应该看看这里得到的输出:Assign output of os.system to a variable and prevent it from being displayed on the screen
这里了解POSIX退出代码:https://en.wikipedia.org/wiki/Exit_status
其实,如果你真的只需要在Python的文件的数量,试试这个:
import pathlib
p = pathlib.Path('.')
files = [x for x in p.iterdir() if x.is_file() and x.match('*.csv')]
print("Number of files: {0}".format(len(files)))
更容易,Pythonic和你不需要分叉一个子过程。
同时检查'subprocess'模块。它最近在'os.system'上是首选。 – orangeInk
而“最近这些日子”,我们的意思是“自从近13年前发布以来”。 – chepner