제가 일하는 회사에 속한 특정 외부 응용 프로그램을 사용하는 Python 스크립트를 작성하려고합니다. 프로그래밍 및 스크립팅에 관해서는 일반적으로 나 자신을 위해 생각할 수 있지만, 이번에는 정말 잃어버린 것입니다!While 루프가 주 기능을 수행하지 않습니다.
나는 왜 while 루프가 작동하지 않는지 알아낼 수 없습니다. 그것은 나를 돕지 않는 어떤 오류도주지 않습니다. 루프의 중심에있는 코드의 중요한 부분을 건너 뛰고 나서 "카운트"를 증가시키는 것처럼 보입니다. "F에서 2 호선 [수]에 대해 :"
f = open('C:/tmp/tmp1.txt', 'w') #Create a tempory textfile
f.write("TEXTFILE\nTEXTFILE\nTEXTFILE\nTEXTFILE\nTEXTFILE\nTEXTFILE\n") #Put some simple text in there
f.close() #Close the file
count = 0 #Insert the line number from the text file you want to begin with (first line starts with 0)
num_lines = sum(1 for line1 in open('C:/tmp/tmp1.txt')) #Get the number of lines from the textfile
f = open('C:/tmp/tmp2.txt', 'w') #Create a new textfile
f.close() #Close it
while (count < num_lines): #Keep the loop within the starting line and total number of lines from the first text file
with open('C:/tmp/tmp1.txt', 'r') as f: #Open the first textfile
line2 = f.readlines() #Read these lines for later input
for line2[count] in f: #For each line from chosen starting line until last line from first text file,...
with open('C:/tmp/tmp2.txt', 'a') as g: #...with the second textfile open for appending strings,...
g.write("hello\n") #...write 'hello\n' each time while "count" < "num_lines"
count = count + 1 #Increment the "count"
나는 모든 때까지 작동 생각
내가 일하고 있어요 진짜 코드는 좀 더 복잡하고, 응용 프로그램 나는 'ISN을 사용하고 있습니다 t는 공유하기 때문에 코드를 간소화하여 문제를 해결할 수 있습니다.
대체 코드를 찾고 있지 않습니다. 루프가 작동하지 않는 이유를 찾고있어 직접 수정하려고 할 수 있습니다.
모든 답변을 주시면 감사하겠습니다. 미리 감사드립니다.
코맥
f :'에 줄 수 [for]를 사용할 수 있습니까? ''for line in f : '을 해봤습니까? – Wug
허. 문제는'readlines()'호출을 한 후에'f'가 더 이상 읽을 줄이 없다는 것인데,'line2 [count]'에'None'을 넣는다는 것입니다. – cha0site
아마도'line2 [count] in f'와 관련이 있습니다. line2를 만들면'for line2 in'을 사용해야합니다. – purpleladydragons