I want to check every single line of an txt file to see if the serial numbers of connected devices existed. Here is my code:
from ppadb.client import Client as AdbClient
# Open the file in append & read mode ('a+')
def checkDeviceName(devicename):
with open('deviceList.txt', "a+") as f:
check = 0
for line in f:
if devicename == line:
check += 1
if check == 0:
f.write('\n')
f.write(devicename)
else:
print('Device Name: ', devicename)
client = AdbClient(host="127.0.0.1", port=5037)
devices = client.devices()
listOutput = []
for device in devices:
output = device.shell("getprop | grep -e 'serialno'")
print(output)
listOutput.append(output[21:35])
print(listOutput)
i = 0
while i < len(listOutput):
checkDeviceName(listOutput[i])
i += 1
The problem is even when the serial numbers of real devices connected is already existed in deviceList.txt file, the program still append it at the end of file. I tried to print out check variable but it always remains at 0. I think the problem is the code can't change the check variable from inside for loop, but I don't know how to fix it. Can you please help me out ? Sorry if my English make any misunderstood.