我有一个简单的问题,但无法真正解决。我在 groupby 循环中有以下代码。对于每个文件,python 在我的目标文件夹上创建文件夹\user\Desktop\CO\Sites,但我只想找到路径并将我的 zip 文件放在该路径上,而不是再次创建。 可以请您指教吗?
对于 groupby 中的 n,g:
csv=g.to_csv(index=False)
filename = '{}{}'.format(r'C:/Users/Desktop/CO/Sites/Site_',n)
os.chdir(r'C:\Users\Desktop')
filename_csv = filename + '_Co_'+ '.csv'
filename_zip = filename + '_Co_' +'.zip'
with open(filename_csv,'w') as out_file:
out_file.write(csv)
zip_all_zips.append(filename_zip)
zip_all_csvs.append(filename_csv)
最佳答案
也许您可以检查该目录是否存在,否则创建它:
if not os.path.exists("filepath/"):
关于Python创建额外的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59269624/