我正在使用 python3.6 自动查找计算机中的一些文件:
import os
criteria = 'something'
file_names = os.popen('find dir -name "*%s*"' % criteria).read()
for file_name in file_names:
#do something
pass
问题是,通过执行 .read(),所有文件名(包括某些条件)都会连接成一个字符串。 如何获取文件名列表作为输出以便在“for”循环中迭代? 非常感谢
最佳答案
在一般情况下,您需要拆分字符串。
最简单的方法是使用Python字符串内置的.split()
方法。
示例:
>>> ' 1 2 3 '.split()
['1', '2', '3']
更多详情,请参阅 the documentation
关于python - 拆分 os.popen 的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50797440/