Why is argparse not recognizing my input file?

Question:

This is my first attempt at writing something with argparse and I am really lost. The goal of this script is to read file.sdf, then write it back out as file2.sdf

This is my script:

import argparse
import rdkit as rdkit
from rdkit.Chem import PandasTools
import pandas as pd

parser = argparse.ArgumentParser(description='This is a work in progress')
parser.add_argument("-i", "--input", help="path to input sdf file")
parser.add_argument("-o", "--output", help="path to output sdf file")
args = parser.parse_args()

df = rdkit.Chem.PandasTools.LoadSDF(r"args.input") 
PandasTools.WriteSDF(df, r"args.output", properties=list(df.columns))

When I run this script like

python script.py --input file.sdf --output file2.sdf

I get this error

File "C:Userslkv206Anaconda3envsrdkitlibsite-packagesrdkitChemPandasTools.py", line 456, in LoadSDF
    f = open(filename, 'rb')
FileNotFoundError: [Errno 2] No such file or directory: 'args.input'

If I open and run a jupyter notebook with this code:

import rdkit as rdkit
from rdkit.Chem import PandasTools
import pandas as pd

df = rdkit.Chem.PandasTools.LoadSDF(r"file.sdf")
PandasTools.WriteSDF(df, r"file2.sdf", properties=list(df.columns))

It successfully gives me the desired output, file2.sdf

So it seems like the code works without argparse, but I can’t get it to work with argparse. I’m guessing I did something wrong in

parser.add_argument

or how I called it later.

I was working off this tutorial: https://www.youtube.com/watch?v=cdblJqEUDNo&ab_channel=JohnnyMetz and can’t understand where I went wrong

Asked By: DK_chemistry

||

Answers:

args is an object. Try:

import argparse
import rdkit as rdkit
from rdkit.Chem import PandasTools
import pandas as pd

parser = argparse.ArgumentParser(description='This is a work in progress')
parser.add_argument("-i", "--input", help="path to input sdf file")
parser.add_argument("-o", "--output", help="path to output sdf file")
args = parser.parse_args()

df = rdkit.Chem.PandasTools.LoadSDF(args.input) 
PandasTools.WriteSDF(df, args.output, properties=list(df.columns))
Answered By: 1extralime
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.