【发布时间】:2013-11-29 01:14:31
【问题描述】:
我是 python 新手。在一个文件中有不同的端口号。我想遍历端口号。端口用逗号分隔。最后,我想在该文件中附加我的端口号。我写的代码不起作用,因为最后总是有一个换行符。我怎么解决这个问题。并且有没有更好的解决方案。这是我的代码 -
f = open("ports.txt", "r")
line = f.readline()
line = line.split(",")
print(line)
if len(line) > 0:
del line[-1]
for port in line:
print(port)
f = open("ports.txt", "a")
m = str(self.myPort)+","
f.write(m)
f.close()
【问题讨论】:
标签: python file file-io python-3.x