私の目には、2番目のforループを入力する必要がありますが、それは決してありません。どうしてこれなの?それはファイルが空であるようですが、私はそれがあなたでないことを保証することができます。forループがファイルを反復しない
デフremove_none():
# remove contents of temp.txt
file = open('temp.txt', 'w')
file.truncate()
file.close()
with open("temp.txt", "a") as temp:
with open("temp_copy.txt") as temp_copy:
num_of_lines = 0
other_IPs = 0
# count the number of lines in temp_copy.txt
for _ in temp_copy:
num_of_lines += 1
other_IPs = num_of_lines-3
print "\nThere are {} IP's excluding router and you.\n".format(other_IPs)
count = 0
os.system("cat temp_copy.txt")
**# this is the second for loop**
for line in temp_copy:
count =+ 1
print count
if count == 1:
# run this on the first line
temp.write(line)
elif count == num_of_lines:
# run this on the last line
# remove the last line
pass
else:
# run this on every other line
line = line[4:]+"\n"
temp.write(line)
2番目のforループの直前と直後にprintステートメントが追加されていますので、入力していないことがわかります。それは単にループを通過し、プログラムを続行します。また、エラーは発生しません。 – Aaron
可能な複製:[https://stackoverflow.com/questions/10255273/iterating-on-a-file-using-python](https://stackoverflow.com/questions/10255273/iterating-on-a-file -using-python) – vealkind
あなたはtemp_copy.txtを投稿できますか? –