2014-12-06 2 views
0

Iam이 직접 텍스트 파일에 C 코드를 작성하고 수동으로 하나 만들지 않고도 확장자를 ".c"로 변경하는 Python 스크립트를 작성하려고합니다. 이것은 내가 지금까지 한 일입니다.파이썬에서 입력을 받아 여러 줄을 텍스트 파일에 쓰고 싶습니까?

import time as t 
from os import path 

def create_C(dest): 

    date=t.localtime(t.time())    #getting current system date 
    name=raw_input("Enter file name:")+"%d_%d_%d"%(date[0],date[1],date[2])+".c" 
                  #User enters the filename 

    if not(path.isfile(dest+name)):   #checks if the file already exists 
     f=open(dest+name,"w") 
     f.write(raw_input("Start Writting:\n")) 
     f.close() 
    if __name__=='__main__': 
     destination='C:\\Users\\Dell\\Desktop\\Puru\ 
\\python\\intermediate\\createCfile\\' 
     create_C(destination) 
     raw_input("done") 

그러나 이것은 단 한 줄만 씁니다. 어떻게 여러 줄을 쓸 수 있습니까? 대신 한 번 raw_input을 호출

답변

0

:

f.write(raw_input('Start writing')) 

당신은 빈 문자열이 삽입 될 때까지 raw_input을 호출 할 수 있습니다

file_content = '\n'.join(iter(raw_input, '')) 
f.write(file_content) 

리틀 명확 :

file_lines = iter(raw_input, '') # Get lines as input until an empty line. 
file_content = '\n'.join(file_lines) # Join the file lines to one string, seperated by new-line. 
+0

감사합니다, 당신은 또한 설명 할 수 약간의 일이 여기 ** file_content = '\ n'.join (iter (raw_input, '')) ** – purudpd

+0

설명을 추가했습니다. . @ PurusharthDwivedi –