我对Python很陌生,现在已经挖了大约一个月了。我一直试图找出这段代码一段时间,但无法弄清问题是什么。我试图强调价值观,我仍然得到错误。一个小程序背景故事。该程序基本上会查看包含数字列表的文件,然后让用户选择要搜索的文件夹,并查看几个文件以查找提供的数字列表。首先,它查找标题记录,然后查找某个位置以查看数字是否匹配,然后将剩余的行复制到新文件。陷入困境的代码是循环应该退出的时候,即它不再是一个订单。
知道我在这里做错了什么:
def read_pos():
with open(os.path.realpath(BrowsePath.get())) as PONumbers:
PO = [line.rstrip('\n') for line in open(os.path.realpath(BrowsePath.get()))] #isolates list of POs
ponum_count = sum(1 for line in open(os.path.realpath(BrowsePath.get()))) #gets count of PO numbers
while ponum_count > 0:
global ponum
print(PO[ponum])
fs = 0
files = os.listdir(SearchPath.get()) #gets list of files
file_count = len(fnmatch.filter(os.listdir(SearchPath.get()),'*.*')) #gets count of files
while file_count > 0:
with open(os.path.join(SearchPath.get(),files[fs]),'r') as content_file:
po_line = [line.rstrip('\n') for line in open(os.path.realpath(os.path.join(SearchPath.get(),files[fs])))] #isolates each file
line_count = sum(1 for line in open(os.path.realpath(os.path.join(SearchPath.get(),files[fs])))) #gets count of lines in PO File
po1 = 0
while po1 < line_count:
if po_line[po1][:2] == "10":
if po_line[po1][73:90].strip() == PO[ponum]:
filename = os.path.abspath(os.path.join(SearchPath.get(),"Results.txt"))
NewPOFlag = "N"
with open(filename,'a+') as file:
file.write(po_line[po1] + '\n')
print(po_line[po1])
#AREA I'M HAVING TROUPBLE WITH
while NewPOFlag == "N":
po1 +=1
if (po_line[po1][:2] == "10") | (po_line[po1] == '\n'):
NewPOFlag = "Y"
else:
with open(filename,'a+') as file:
file.write(po_line[po1] + '\n')
print(po_line[po1])
#line_count = line_count - 1
#po1 +=1
print(files[fs])
fs +=1
file_count = file_count - 1
ponum +=1
ponum_count = ponum_count - 1
这是我的追溯结果:
File "C:\Python\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "C:\Users\USER\Desktop\PythonTesting\FindPOs.py", line 124, in read_pos
if (po_line[po1][:2] == "10") | (po_line[po1] == '\n'):
IndexError: list index out of range