2016-08-29 3 views
1

임의의 음계를 선택하는 프로그램을 작성하려고합니다. 문제는 내 코드가 한 줄만 내 텍스트 파일에 쓰고 있다는 것입니다.파이썬은 마지막 출력을 파일에만 씁니다.

나는이 질문이 Python: Only writes last line of output과 비슷한 질문이지만 이미 루프를 벗어난 파일을 열어 닫으려고 시도했다. .

내 코드는 이것이다 :

#imports the required library 
import random  

#picks 1 hands separate major scale 
def MajorScalesHandsSeparate(): 

    #initialises the chars variable 
    chars = 0 

    #creates the checker file if it has not previously been created 
    while True: 
     with open('MajorScalesHandsSeparate.txt', 'a') as f: 
      break 

    #counts the number of chars in the file 
    with open('MajorScalesHandsSeparate.txt', 'r') as f: 
     for line in f: 
      chars += len(line) 

    #converts file to list 
    with open('MajorScalesHandsSeparate.csv', 'r') as f: 
     MajorScalesHandsSeparate = [line.strip() for line in f] 

    #opens file to check for the number of lines 
    with open('MajorScalesHandsSeparate.csv', 'r') as f: 
     Items = sum(1 for _ in f) 

    #asks the user how many scales they would like 
    NumScales = input("How many hands separate major scales would you like? ") 

    #resets the loop counter and picker to 0 
    WhileControl = 0 
    ScalePicker = 0 

    '''HERE IS WHERE I BELIEVE I FOLLOWED THE LINKED QUESTION''' 
    checker = open('MajorScalesHandsSeparate.txt', 'w+') 
    #choses a number 
    while WhileControl != NumScales: 
     ScalePicker = random.randint(0, Items-1) 

     #checks if scale has already been chosen 
     if MajorScalesHandsSeparate[ScalePicker] not in open('MajorScalesHandsSeparate.txt').read(): 

      #writes scale to file 
      Scale=str(MajorScalesHandsSeparate[ScalePicker]) 
      checker.seek(chars) 
      checker.write(Scale + '\n') 

      #prints chosen scale 
      print MajorScalesHandsSeparate[ScalePicker] 

      #increments the loop counter by one 
      WhileControl = WhileControl + 1 

      #removes item from list  
      else: 
       MajorScalesHandsSeparate.remove(MajorScalesHandsSeparate[ScalePicker]) 
       Items = Items - 1 

     #checks if all scales have been used 
     if len(MajorScalesHandsSeparate) == 0: 
      with open('MajorScalesHandsSeparate.csv', 'r') as f: 
       #converts file to list once again 
       MajorScalesHandsSeparate = [line.strip() for line in f] 

    #closes the file 
    checker.close() 

#calls the function 
MajorScalesHandsSeparate() 

내 출력은 다음과 같습니다

How many hands separate major scales would you like? 3 
Db major RH only 
F# major LH only 
F# major RH only 
>>> 

그러나 텍스트 파일 읽기 :

F# major RH only 

나는 그것을 같이 할 :

Db major RH only 
F# major LH only 
F# major RH only 
+0

를 한 번 설정하고 업데이트되지 않습니다 아주 좋은되지 않은 :이 때문이다. 'MajorScalesHandsSeparate [ScalePicker]가 열려 있지 않다면 ('MajorScalesHandsSeparate.txt') read()' –

+0

55 번째 줄과 56 번째 줄에 들여 쓰기 오류가 있습니다. –

+0

@ Jean-FrançoisFabre 다시 쓰기 위해 파일을 열어야합니까? ? – trunting

답변

1

코드는 출력 파일의 같은 위치에 쓰고 덮어 씁니다.

checker.seek(chars) 
checker.write(Scale + '\n') 

chars가 쓰는 동안 파일을 읽는

+0

대단히 감사합니다. :) – trunting

관련 문제