2014-05-20 1 views
0

일부 iOS 코드에 메서드를 추가하는 python 스크립트를 작성하고 있습니다. 특정 줄의 파일을 검사 한 다음 그 줄 다음에 파일에 쓰는 스크립트가 필요합니다. 예를 들어 :파일의 특정 줄을 가져온 다음 해당 줄 다음에 글자 쓰기 시작

  • 스크립트는이 라인을

의 #pragma 마크가 발생 - 다음이 줄 끝에서 방법을 쓰는 방법

전에서 그렇게 할 수있는 방법 파이썬?

감사합니다.

콜린

+0

에 지금까지 노력이 무엇인지에 대한 언급하세요? –

답변

1

나는 당신이 실제로 귀하의 질문에서 알 수 있듯이 #pragma 마커 다음에 오는 것도 이상 쓰고 싶지 않아 있으리라 믿고있어.

marker = "#pragma Mark - Method\n" 
method = "code to add to the file\n" 

with open("C:\codefile.cpp", "r+") as codefile: 
    # find the line 
    line = "" 
    while line != marker: 
     line = codefile.readline() 
    # save our position 
    pos = codefile.tell() 
    # read the rest of the file 
    remainder = codefile.read() 
    # return to the line after the #pragma 
    codefile.seek(pos) 
    # write the new method 
    codefile.write(method) 
    # write the rest of the file 
    codefile.write(remainder) 

이 파일의 텍스트의 나머지 부분을 덮어 쓸 경우, 즉 간단 :

with open("C:/codefile.cpp", "r+") as codefile: 
    # find the line 
    line = "" 
    while line != marker: 
     line = codefile.readline() 
    # write the new method 
    codefile.write(method) 
    # erase everything after it from the file 
    codefile.truncate() 
+0

감사합니다. 실제로 다른 솔루션을 생각해 냈습니다.하지만이 것은 마크 이후에 나오는 것을 제거하지 않는 것이 좋습니다. 감사! –

관련 문제