2014-04-23 5 views
0

다음 코드를 작성하기 위해 특정 위치에 파일을 쓰려고합니다.특정 위치에 CSV 파일을 쓰려고하는 Python입니다.

프로그램이 외부 HDD의 폴더에 있습니다. 내가 = 현재 경로 (내가 생각하는 ..)

은 "fileName에"VAR이를 얻을 수있는을 os.path를 사용한 안녕하세요 은 "savePath"VAR 내가 얻을 코드를 실행하면 = 데이터

입니다 다음과 같은 오류가 ...

오류 IOError : [errno를 13] 권한이 거부 : '데이터를 \ hello_23-04-2014_13-37-55.csv'

내가 시도하기 전에 파일에 대한 권한을 설정해야합니까 그것에 쓰려면? 그렇다면 어떻게해야합니까>?

:이 작업을 수행 할 때

>>> import os 
>>> path = "foo/bar/file.txt" 
>>> os.makedirs(path) 
>>> with open(path, "w") as f: 
... f.write("HOWDY!") 
... 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
IOError: [Errno 21] Is a directory: 'foo/bar/file.txt' 

참고 : 내가 옷을 벗었 여기서 무슨 일을하는지의 버전을하려고 할 때

def writeData(fileName, savePath, data): 
    # Create a filename 
    thisdate = time.strftime("%d-%m-%Y") 
    thistime = time.strftime("%H-%M-%S") 
    name = fileName + "_" + thisdate + "_" + thistime + ".csv" 

    # Create the complete filename including the absolute path 
    completeName = os.path.join(savePath, name) 

    # Check if directory exists 
    if not os.path.exists(completeName): 
     os.makedirs(completeName) 

    # Write the data to a file 
    theFile = open(completeName, 'wb') 
    writer = csv.writer(theFile, quoting=csv.QUOTE_ALL) 
    writer.writerows(data) 
+1

글을 쓰고있는 * 폴더 *에 대한 충분한 권한이없는 것일 수 있습니까? –

답변

2

나는 (옆 권한 문제를두고) 다른 오류
# Check if directory exists 
if not os.path.exists(completeName): 
    os.makedirs(completeName) 

... 원하는 경로와 만들려는 파일의 이름을 모두 가진 디렉토리를 만듭니다. 경로 이름을 makedirs()으로 전달한 다음 완료하면 해당 디렉토리 내에 파일을 작성하십시오.

+0

고마워요, 그게 핵심이었습니다! –

관련 문제