报错的意思是, 没有对应的文件夹。
可能是在上一步操作的时候把路径指定了?
os.chdir(path)
用了网上的方法,先判断是否文件夹存在,如果不存在,就先创建对应的文件夹。
import os
outname = 'filename.csv'
outdir = './dir'
if not os.path.exists(outdir):
os.mkdir(outdir)
fullname = os.path.join(outdir, outname)
df.to_csv(fullname)
还是报同样的错误。
所以就粗暴地先创建一个空的csv文件(在指定了路径的前提下,就不用绝对路径创建文件了),判断文件是否创建成功,如果返回了True,再run接下来的代码,fix error成功了。
with open('filename.csv', 'w') as my_new_csv_file:
pass
## check if the file create sucessfully
os.stat('filename.csv').st_size == 0
## if succeeded, returned True