0

I am using click library in order to call function from one folder and read file and after that to write file.

import sys
import numpy as np
import pandas as pd

import click

def count_unique_port(df: pd.DataFrame) -> pd.DataFrame:    
    df = df.dropna()
    df = df.groupby(['A1', 'A2'], as_index=False).size()
    return port

@click.command(help="Pandas count")
@click.option("-i", "--input", "infile", type=click.File(), default=sys.stdin, help="Input file name")
@click.option("-o", "--output", "outfile", type=click.File("w"), default=sys.stdout, help="Output file name")

def main(infile, outfile):
    df = pd.read_csv(infile)
    temp = count_unique_port(df)
    print(temp)
    temp.to_excel(outfile, index=False)

if __name__ == "__main__":
    
    main()

The problem that I stumble upon is when I call python script python path1/read_data.py ./path/data/f1.csv ./path/data/f2.xlsx

It does not write the pandas dataframe to the folder path. What I am doing wrong? Any help is welcome.

1 Answer 1

0

Because you define Options, not Arguments. Run it as:

python path1/read_data.py -i ./path/data/f1.csv -o ./path/data/f2.xlsx

And you need to specify the output file for "writing":

@click.option("-o", "--output", "outfile", type=click.File("w"), default=sys.stdout, help="Output file name")

note the "w" as a parameter of click.File("w")

Sign up to request clarification or add additional context in comments.

8 Comments

It does not work again.
Please provide more details on what "does not work" means. Do you get an error? Unexpected result? If so, include error/result in your question.
I run the command in the way that you mentioned. It does not save again the dataframe.
Does it show any errors? Or just silently exits without printing anything?
I found a mistake here is the error. Error: Invalid value for '-o' / '--output': './apth/data/f2.xlsx': No such file or directory
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.