这个问题听起来可能是重复的;但这里是解释问题的代码:
#compares partitions displayed by 'df' command with the required list of partitions.
import subprocess
import re
p = subprocess.Popen("df -h", stdout=subprocess.PIPE, shell=True)
dfdata, _ = p.communicate()
dfdata = dfdata.replace("Mounted on", "")
columns = [list() for i in range(10)]
for line in dfdata.split("\n"):
line = re.sub(" +", " ", line)
for i,l in enumerate(line.split(" ")):
columns[i].append(l)
sys_partition = columns[5]
req_partition = ['/', '/boot', '/home', '/usr', '/usr/local', '/var', '/tmp', '/mnt/floppy', '/mnt/cdrom', '/dev/shm']
def remove_common_elements(sys_partition, req_partition):
sys_partition_new = sys_partition[:]
req_partition_new = req_partition[:]
for i in sys_partition:
if i in req_partition_new:
sys_partition_new.remove(i)
req_partition_new.remove(i)
return req_partition_new
if set(sys_partition) == set(req_partition):
print 'All the partitions exist in your system!!!'
else:
print 'These partitions do not exist: ',
print (remove_common_elements(sys_partition, req_partition))
在这个程序中,“sys_partition”表示系统中存在的分区(由 df 命令显示),“req_partition”表示理想情况下应该存在的分区列表。现在,我不想硬编码所需的分区(req_partition)。我想从某个外部文件中获取该输入并将其存储到一个列表中,然后像显示的那样处理它。请帮忙。如果问题令人困惑,我将尝试详细说明。提前致谢