Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why is argparse not recognizing my input file?

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

I get this error

File "C:\Users\lkv206\Anaconda3\envs\rdkit\lib\site-packages\rdkit\Chem\PandasTools.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

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading