2016-09-30 72 views
0

該目錄的位置正確,但未在該文件夾中創建csv文件。未在目錄中創建csv文件

代碼用於創建一個文件夾:

self.directory = os.path.join('Admins/'+name+'/') 
os.makedirs(self.directory) 

代碼:

wfile = open(str(self.directory) + '/' + 'SALES.CSV', 'a+') 

錯誤:

no 'SALES.CSV' found 

回答

0

如果你要依靠os.path.join()你可能想使用它它打算的方式: os.path.join(p1, p2, p3) 沒有任何斜槓。您可能還想使用

with open(os.path.join(self.directory, "SALES.CSV"), 'a+') as wfile: 
    # Process the file as you like, it will close itself afterwards 

介意試試這個並讓我們知道結果?

編輯:剛纔發現了一些東西。

您正在使用'a +',這意味着您以'append'模式打開文件。但是由於您剛剛創建了該目錄,我懷疑該文件不在第一位。如果你想打開一個,只需用'w'代替'a +'即可。

+0

我已經根據上面的答案進行了更改。但是這個結果是部分工作的。 csv文件是在py文件存在的地方創建的,而不是在目錄中。任何人都可以幫助我。 –