コードが2番目のforループに入っていません。私はどこでもファイル記述子を変更していません。なぜそれが起こっているのですか?forループが同じファイル記述子で2回動作しない
import os
import re
path = '/home/ajay/Desktop/practice/ajay.txt'
os.system('ifconfig>>path')
fd = open(path,'r+')
interfaces = []
tx_packets = []
for line in fd:
if(re.search(r'(\w+)\s+\Link',line)):
inter = re.match(r'(\w+)\s+\Link',line)
interface = inter.group(1)
interfaces.append(interface)
for lines in fd:
if(re.search(r'\s+TX packets:(\d+)',lines)):
tx_packs = re.match(r'\s+TX packets:(\d+)',lines)
tx_pack = tx_packs.group(1)
tx_packets.append(tx_pack)
print interfaces
print tx_packets
を使用してください。 'for lines in fd:'は、ファイルの最後に読み込みポインタなどを置くためです。あなたの 'for 'ループの間で' fd.seek(0) 'を呼び出す – jDo