如何将导出的文件保存到与py文件相同的路径,并将我的csv文件重命名为文件夹名称



我试图将html文件刮擦到csv文件,我想将导出的文件保存到与py文件相同的路径,并将我的csv文件重命名为文件夹名称。怎么做呢?

import pandas as pd
import os
directoryPath = r"./"
os.chdir(directoryPath)
folderLister = os.listdir(directoryPath)
for folders, sub_folders, file in os.walk(directoryPath):
for name in file:
if name.endswith(".html"):
filename=os.path.join(folders,name)
print(filename)
print(name)
dfs = pd.read_html(filename)
html_filename = filename.replace('.html', '.csv')
dfs[3].to_csv(html_filename)

检查当前路径

import pathlib
current_path = pathlib.Path(__file__).parent.resolve()
from pathlib import Path  
FILE = Path(__file__) #give you the py file name
FILE.absolute() #give you the absolute path of the py file
FILE.parents[1].as_posix() #give you the folder name

相关内容

  • 没有找到相关文章

最新更新