2013-06-02 6 views

답변

3

os.open 반환 파일 설명 : 여기에

내 스크립트 여기

data = urllib.urlopen(swfurl) 

     save = raw_input("Type filename for saving. 'D' for same filename") 

     if save.lower() == "d": 
     # here gives me Attribute Error 

      fh = os.open(swfname,os.O_WRONLY|os.O_CREAT|os.O_TRUNC) 
      fh.write(data) 

     # ##################################################### 

의 일부 것은 오류입니다. 당신은 어떤 낮은 수준의 API

with open('foo.txt', 'w') as output_file: 
    output_file.write('this is test') 
1

os.open() 파일 기술자 (정수)를 반환 필요하지 않은 경우 열린 파일에

import os 
# Open a file 
fd = os.open("foo.txt", os.O_WRONLY | os.O_CREAT | os.O_TRUNC) 
# Write one string 
os.write(fd, "This is test") 
# Close opened file 
os.close(fd) 

또는보다 효율적으로 사용 파이썬 파일을 작성하는 os.write를 사용하지 않는 파일 객체 . docs에서 :

fh = open(swfname, 'w') 
fh.write(data) 
fh.close() 

또는 컨텍스트 관리자 :

Note: This function is intended for low-level I/O. For normal usage, use the built-in function open() , which returns a “file object” with read() and write() methods (and many more). To wrap a file descriptor in a “file object”, use fdopen() .

대신 내장 open() 기능을 사용해야합니다

with open(swfname, 'w') as handle: 
    handle.write(data) 
+0

이 상황에서을() 내장 개방은 적절하지 않다 플래그 O_WRONLY, O_CREAT 및 O_TRUNC를 전달하는 데 필요한 저수준 API가 없기 때문입니다. 포스터는 os.open()을 사용하는 것이 옳습니다. 이 메 커니 즘에 익숙하지 않은 경우에는 내장 된 open()과 os.open()을 개괄하고 os.open()을 사용할시기에 대한 지침을 제공하는 @ oleg의 대답을 참조하십시오. –

관련 문제