[英]Saving files to different folder
I´m trying to save converted excel files from different paths, to the same folder.我正在尝试将转换后的 excel 文件从不同的路径保存到同一个文件夹。 How can I pass the path to the function correctly?
如何正确传递 function 的路径? Now what is happening is that it is attaching the original path to the save path I have given to the function.
现在发生的事情是将原始路径附加到我提供给 function 的保存路径。
I get the following error (of course because there is no such path):我收到以下错误(当然是因为没有这样的路径):
OSError: Cannot save file into a non-existent directory: 'csv_files\excel_files\PLM'
import pandas as pd
import glob
import csv, json
import openpyxl
from pathlib import Path
import os, os.path
import errno
save_path = "./csv_files"
for path in ["excel_files/PLM", "excel_files/PTR", "excel_files/TMR"]:
for xlsx_file in glob.glob(os.path.join(path,"*.xlsx*")):
data_xls = pd.read_excel(xlsx_file, 'Relatório - DADOS', index_col=None,
engine='openpyxl')
csv_file = os.path.splitext(xlsx_file)[0]+".csv"
data_xls.to_csv(os.path.join(save_path, csv_file), encoding='utf-8', index=False)
You can ensure the save folder exists by adding this line before the outer for
loop:您可以通过在外部
for
循环之前添加此行来确保保存文件夹存在:
Path(save_path).mkdir(exist_ok=True)
See documentation .请参阅文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.