대신 이 파일에 대한 오버라이트합니다 덮어쓰기/추가 어떻게 해야 할까요? 특수 기능을 제공하는 파일 첨부됩니다 있나요?
파일의 덮어쓰기/추가 모드를 설정하면, a" " 열어야 합니다. 또는 " ab"; 같은 모드. 지켜보리니 [open ()] [1] .
함께 열면 " a"; , 항상 쓰기 포지셔닝하십시오 모드 파일 끝 부분에 (append) 될 것이다. &Quot a+", 함께 열 수 있습니다. 거꾸로, 읽을 수 있도록 판독값 말라실로 (단, 모든 쓰기 작업은 계속 파일 끝 부분에!).
예:
>>> with open('test1','wb') as f:
f.write('test')
>>> with open('test1','ab') as f:
f.write('koko')
>>> with open('test1','rb') as f:
f.read()
'testkoko'
몇 가지 방법에 대한 자세한 내용은 " a"; 모드 () 는 ( 테스트됨 Linux 에서만 ). 모든 write 는 덮어쓰기/추가 뒤로를 말라실로 경우에도 끝까지 파일:
>>> f = open('test','a+') # Not using 'with' just to simplify the example REPL session
>>> f.write('hi')
>>> f.seek(0)
>>> f.read()
'hi'
>>> f.seek(0)
>>> f.write('bye') # Will still append despite the seek(0)!
>>> f.seek(0)
>>> f.read()
'hibye'
실제로 'fopen' 마 페이지 다음과 같은 뜻이 있다.
>. 덮어쓰기/추가 모드 (mode) 의 첫 번째 문자는 a 가) 에서 파일 열기 >. 이 때문에 이후의 모든 쓰기 작업을 발생하도록 스트리밍합니다 >. 파일 끝, 앞에 있는 것처럼 전화: >. >. 할당된 block 의 수 (스트리밍합니다 SEEK_END, 0);
예: ( 한 진정한 프로그램 사용 '을 (를)' [문서] [3] - 닫으십시오 파일 참조)
>>> open("test","wb").write("test")
>>> open("test","a+b").write("koko")
>>> open("test","rb").read()
'testkoko'
[1]: https://docs.python.org/2/library/functions.html # 오픈
[3]: http://docs.python.org/2/whatsnew/2.6.html # pep 를 343 기술서임을
좋습니다, ',' 를 전달하기 위해 " a" 모드 인수. 문서 참조 장치당 [open ()] [1].
with open("foo", "a") as f:
f.write("cool beans...")
이외에도 모드 (+) 의 순열 (w), 이진 (b), 잘라내는 업데이트하던 한다는 주장이 있지만, ',' is your best 내기하죠 a" " 부트해 모드 시작
Python 은 여러 가지 변형이 끄기도구 기본 3 모드, 이러한 세 가지 모드가 있다:
'w' write text
'r' read text
'a' append text
그래서 첨부하려면 파일로 it& # 39 의 것만큼 쉽다.
f = open('filename.txt', 'a')
f.write('whatever you want to write here (in append mode) here.')
그럼 그건 모드 코드를 만들 줄 수가 있습니다.
'r+' read + write text
'w+' read + write text
'a+' append + read text
마지막으로, 읽기 / 쓰기를 바이너리 형식의 모드 있습니다.
'rb' read binary
'wb' write binary
'ab' append binary
'rb+' read + write binary
'wb+' read + write binary
'ab+' append + read binary
하나님이 이 선을 사용하여 '오픈 (파일_이름, a" ";)', 'a' 는 추가할 수 있는 파일, 즉, 기존 파일 추가 데이터 삽입하려면.
그냥 텍스트 파일에 사용할 수 있습니다 다음 흐름선 첨부하려면
def FileSave(filename,content):
with open(filename, "a") as myfile:
myfile.write(content)
FileSave("test.txt","test1 \n")
FileSave("test.txt","test2 \n")
만약 파일 첨부하려는
with open("test.txt", "a") as myfile:
myfile.write("append me")
'우리' 라는 이름의 파일을 열 수 있는 '' 테스트리트스트 가변적입니다 myfile 선언했다. 오픈 2 는 인수, 우리가 원하는 파일을 열고 문자열으로 종류를 나타내는 작업에 대한 허가 또는 우리가 바라는 file< br>;
다음은 모드 옵션을 파일 < pre>; 모드 설명
만약 그 파일이 없으면 새 파일을 만듭니다. 만약 그 파일이 잘라냅니다 절실해졌습니다.
a& # 39, & # 39 에서 파일 열기, 덮어쓰기/추가 모드. 만약 그 파일이 없으면 새 파일을 만듭니다.
b& # 39, & # 39, 이진 모드에서 이 열립니다.
< /pre>;
A& # 39, & # 39 를 의미하는 ',' 매개변수입니다 덮어쓰기/추가 모드. # 39 don& 사용할 경우, t '개방' 함수를 작성할 때마다 당신꺼에요 쉽게 할 수 있습니다. "' 데프 덮어쓰기/추가 (# 39, & # 39, txt = 성공적으로 Executed& \nFunction, 파일): 열린 마음으로 (파일, & # 39, a& # 39;) tpc. f: f.라이트 (txt) "' 이외의 다른 곳에 쓸 스케쳐내 # 39, & # 39 및끝 사용할 수 있습니다. ',' r+& < sup> † /sup> <;: "' 가져오기의 os
열린 마음으로 (파일, & # 39, r+& # 39;) tpc. f: 프리스크 (0, os. SEEK_END) f.라이트 (텍스트 add" ";) "'
shortPath = "../file_to_be_appended"
short = open(shortPath, 'r')
## this counts how many line are originally in the file:
long_path = "../file_to_be_appended_to"
long = open(long_path, 'r')
for i,l in enumerate(long):
pass
print "%s has %i lines initially" %(long_path,i)
long.close()
long = open(long_path, 'a') ## now open long file to append
l = True ## will be a line
c = 0 ## count the number of lines you write
while l:
try:
l = short.next() ## when you run out of lines, this breaks and the except statement is run
c += 1
long.write(l)
except:
l = None
long.close()
print "Done!, wrote %s lines" %c
## finally, count how many lines are left.
long = open(long_path, 'r')
for i,l in enumerate(long):
pass
print "%s has %i lines after appending new lines" %(long_path, i)
long.close()