我正在尝试列出目录中包含我指定的字符串作为其名称一部分的所有文件。我想在循环的每次迭代中改变这个字符串。我正在使用的代码是:
from subprocess import Popen
from subprocess import call
species_array = ["homo_sapiens", "pan_troglodytes", "pongo_abelii", "gorilla_gorilla", "macaca_mulatta", "callithrix_jacchus", "bos_taurus", "canis_familiaris", "equus_caballus", "felis_catus", "ovis_aries", "sus_scrofa", "oryctolagus_cuniculus", "rattus_norvegicus", "mus_caroli", "mus_pahari", "mus_musculus"]
run_length = (len(species_array) - 5)
path = "/homes/varshith/maf_files/1/testmafs/HAL_Files/"
for i in range (run_length):
s = Popen("find", path, "-name", *species_array[i+1]*)
print s.communicate()[0]
该文件应包含species_array [i + 1]作为其名称的一部分。提前谢谢。
答案 0 :(得分:2)
如果您想使用find
,则需要在list
时传递args
shell=False
。 check_output
适用于您的情况,您可以对列表进行切片而不是使用范围,并且需要str.format
来包装*
中的每个项目/元素:
from subprocess import check_output
species_array = ["homo_sapiens", "pan_troglodytes", "pongo_abelii", "gorilla_gorilla", "macaca_mulatta", "callithrix_jacchus", "bos_taurus", "canis_familiaris", "equus_caballus", "felis_catus", "ovis_aries", "sus_scrofa", "oryctolagus_cuniculus", "rattus_norvegicus", "mus_caroli", "mus_pahari", "mus_musculus"]
path = "/homes/varshith/maf_files/1/testmafs/HAL_Files/"
for ele in species_array[1:-5]:
s = check_output(["find", path, "-name", "*{0}*".format(ele)])
print s
对于python 2.6,请使用Popen:
from subprocess Popen,PIPE
species_array = ["homo_sapiens", "pan_troglodytes", "pongo_abelii", "gorilla_gorilla", "macaca_mulatta", "callithrix_jacchus", "bos_taurus", "canis_familiaris", "equus_caballus", "felis_catus", "ovis_aries", "sus_scrofa", "oryctolagus_cuniculus", "rattus_norvegicus", "mus_caroli", "mus_pahari", "mus_musculus"]
path = "/homes/varshith/maf_files/1/testmafs/HAL_Files/"
for ele in species_array[1:-5]:
s = Popen(["find", path, "-name", "*{0}*".format(ele)],stdout=PIPE,stderr=PIPE)
out,err = s.communicate()
print(out,err)
答案 1 :(得分:2)
你的循环都错了。 python比那更具表现力:
1)您可以通过以1:
开始范围来跳过第一个元素for i in range(1, len(species_arr) - 4):
...然后在循环中使用i
而不是i+1
。
2)使用列表切片更简单(也更惯用):
for species in species_arr[1:-4]:
3)您可以使用format()方法在python中格式化字符串。
以下是使用这些概念的示例:
species_arr = [
"homo_sapiens",
"pan_troglodytes",
"pongo_abelii",
"gorilla_gorilla",
"macaca_mulatta",
"callithrix_jacchus",
"bos_taurus",
"canis_familiaris",
"equus_caballus",
"felis_catus",
"ovis_aries",
"sus_scrofa",
"oryctolagus_cuniculus",
"rattus_norvegicus",
"mus_caroli",
"mus_pahari",
"mus_musculus"
]
chop_from_end = 4
for species in species_arr[1:-chop_from_end]:
fname = "*{0}*".format(species)
print fname
--output:--
*pan_troglodytes*
*pongo_abelii*
*gorilla_gorilla*
*macaca_mulatta*
*callithrix_jacchus*
*bos_taurus*
*canis_familiaris*
*equus_caballus*
*felis_catus*
*ovis_aries*
*sus_scrofa*
*oryctolagus_cuniculus*
format()方法是在python 3.0中引入的 - 但它被反向移植到python 2.6(以更有限的形式)。如果由于某种原因你的安装没有format()方法,你可以使用旧方法:
fname = "*%s*" % species
请参阅此处的其他格式()示例:
https://docs.python.org/3/library/string.html#format-examples
4)以下是glob module
:
import glob
import os.path
import pprint
base_dir = '/Users/7stud/python_programs/dir1'
names = ['a', 'b', 'c']
for name in names:
fname = "*{0}*".format(name)
path = os.path.join(base_dir, fname)
pprint.pprint(glob.glob(path))
print '-' * 20
--output:--
['/Users/7stud/python_programs/dir1/__pycache__',
'/Users/7stud/python_programs/dir1/a.txt',
'/Users/7stud/python_programs/dir1/aa.txt',
'/Users/7stud/python_programs/dir1/ab.txt',
'/Users/7stud/python_programs/dir1/ba.txt']
--------------------
['/Users/7stud/python_programs/dir1/ab.txt',
'/Users/7stud/python_programs/dir1/b.txt',
'/Users/7stud/python_programs/dir1/ba.txt']
--------------------
['/Users/7stud/python_programs/dir1/__pycache__']
--------------------
或者,作为name, matches
对的词典:
results = dict(
(
name,
glob.iglob(os.path.join(base_dir, "*{0}*".format(name)))
)
for name in names
)
for name, _iter in results.items():
print "{0}:".format(name)
pprint.pprint(list(_iter))
--output:--
a:
['/Users/7stud/python_programs/dir1/__pycache__',
'/Users/7stud/python_programs/dir1/a.txt',
'/Users/7stud/python_programs/dir1/aa.txt',
'/Users/7stud/python_programs/dir1/ab.txt',
'/Users/7stud/python_programs/dir1/ba.txt']
c:
['/Users/7stud/python_programs/dir1/__pycache__']
b:
['/Users/7stud/python_programs/dir1/ab.txt',
'/Users/7stud/python_programs/dir1/b.txt',
'/Users/7stud/python_programs/dir1/ba.txt']