'checking folder exisits, otherwise create one and write excel to excel into it

I have long analysis during which I need to write the results (tables) to an Excel file. To avoid writing the codes multiple times throughout the analysis, I created function: Its purpose is:

  1. Check if folder exist or not
  2. If folder exist, just write the data frame into an excel file into this folder
  3. If folder not found, create new folder and then write the excel file into it

It used to work but not anymore, now it neither gives an error nor any outputs as if I executed empty cell. I don't understand why, any help is highly appreciated

def dfToExcel(df,filename,sheet_name,index,sub_dir=None):    
                   
    current_path = os.getcwd()
    
    if sub_dir != None:
        path = current_path+'\\'+ sub_dir 
        ispath = Path(path).exists()
        if ispath == False:
           os.mkdir(path) 
        
        file_path= path+'\\'+ filename
    else:        
        file_path=current_path+'\\'+ filename
        
    ispath = Path(file_path).exists()
    if ispath == False:
        df.to_excel(file_path ,sheet_name=sheet_name,index=index,engine = 'xlsxwriter')        
    else:
        with pd.ExcelWriter(file_path,mode='a',engine = 'openpyxl') as writer:    
            df.to_excel(writer,sheet_name=sheet_name, index=index,encoding='iso-8859-1')


Solution 1:[1]

Some issues that could be broken:

  • you are executing the script in another OS/system that uses a different separator, not \\ (backslash, like in Windows, etc.)
  • the input parameters changed, e.g. sub_dir or filename

If you can extract the unit into a function, it is isolated and can be tested. See this minimal, reproducible example:

import os
from pathlib import Path


def create_sub(sub_dir, filename):
    current_path = os.getcwd()
    if sub_dir != None:
        path = Path(current_path, sub_dir)
        ispath = Path(path).exists()
        if ispath == False:
           os.mkdir(path)
        file_path = Path(path, filename)
    else:
        file_path = Path(current_path, filename)
    ispath = Path(file_path).exists()
    if ispath == False:
        return None
    else:
        return file_path


create_sub('hello', 'world.txt')

Note: the function returns None if given file does not yet exist, otherwise the file_path (to filename within created sub_dir).

I first tested it with the hard-coded (file-separtor \\). But since on Linux these are not valid, I changed the path-building to Path(parent, child) form. Then it worked.

To reuse this function: You can integrate it back into your script like:

file_path = create_sub(sub_dir, filename)
if file_path:  # remember the return is either path or None
    df.to_excel(file_path ,sheet_name=sheet_name,index=index,engine = 'xlsxwriter')
else:  # if it was None
    with pd.ExcelWriter(file_path,mode='a',engine = 'openpyxl') as writer:    
        df.to_excel(writer,sheet_name=sheet_name, index=index,encoding='iso-8859-1')

See also

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1