2013-12-15 7 views
2

텍스트 파일을 출력하는 프로그램을 작성하려고합니다. 내가 그것을 실행하는 동안 그것은 이름을 바꿀 것이다.Python : IOError : [Errno 2] 해당 파일이나 디렉토리가 없습니다 (Raspberry PI)

그러나 나는 그것을 실행할 수 없다. 오류 메시지 여기 [IOError: Errno 2 No such file or directory]

내 코드는 다음과 같습니다

from Adafruit_BMP085 import BMP085 
from time import sleep 
import time 
import datetime 
import pickle, sys, os 

while True:  

class data(): 
    def __init__(self): 
     self.tp = tps() 
     self.savedata() 

    def savedata(self):    
     now = datetime.datetime.now() 
     timestamp = now.strftime("%Y/%m/%d %H:%M:%S") 
     filename= 'Temperature_'+ timestamp +'.txt' 

     f=open (filename,'a') 

     timestamp = now.strftime("%Y/%m/%d %H:%M:%S") 

     self.tp.updateTempAndPressure() 

     outvalue = self.tp.temp 
     outvalue_1= self.tp.altitude 
     outvalue_2= self.tp.pressure/100    

     outstring = str(timestamp)+" Temperature:"+str(outvalue)+ " C Altitude: "+str(outvalue_1)+ "m Pressure: " +str(outvalue_2)+ "hPa" + "\n" 

     print outstring 
     f.write(outstring) 
     f.close() 

data() 
time.sleep(1) 

거기에 내가 나 전체 작업을 실행할 수 없습니다 원인이 내 코드를 잘못 다른 있나요 놓치지 어떤 기능이? 문 다음 사전

답변

3

에서

덕분에 '2013/12/15 20:44:59

timestamp = now.strftime("%Y/%m/%d %H:%M:%S") 

/ 같은 문자열을 생성하는 유닉스에서 경로 이름을 구분합니다. 파일을 만들려면 중간 디렉토리가 필요합니다.

>>> open('1/2/3', 'a') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
IOError: [Errno 2] No such file or directory: '1/2/3' 
>>> open('1-2-3', 'a') 
<open file '1-2-3', mode 'a' at 0xb74bd128> 

어떻게 - 같은 다른 문자로 변경 / 어떻습니까?

+0

작품! 제안 해 주셔서 감사합니다. – XFrost

관련 문제