csv文件未在目录中创建

时间:2016-09-30 07:20:11

标签: python-2.7 csv module directory operating-system

目录已正确定位,但未在该文件夹中创建csv文件。

用于创建文件夹的代码:

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

代码:

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

错误:

 no 'SALES.CSV' found

1 个答案:

答案 0 :(得分: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 +'这意味着你打开文件'追加'模式。但是,由于您刚刚创建了目录,我怀疑该文件首先不存在。如果你想打开一个,只需替换' a +'与' w'。

相关问题