Reputation: 29
I have written (with a lot of aid from my friend) a python script that allows me to:
This was done to avoid files getting overwritten when using the cp
command.
Recently I have tried using the script but I now get an error like this:
python ~/gatherfiles.py —outdir protein_files —trgfile ./*/protein.faa`
error:
gatherfiles.py: error: unrecognized arguments: ./GCF_000374525.1_ASM37452v1_genomic.fna_dfast/protein.faa ./GCF_000463742_ASM94337v1_genomic.fna_dfast/protein.faa ./GCF_...etc.
Does anybody know how to overcome this issue?
Here is the actual script:
_all__ = ['gatherfiles']
import os
import glob
import shutil
import argparse
def gatherfiles(outdirpath, trgfilename):
"""
Args:
- outdirpath (str): A path to output directory.
- trgfilename (str): A name of target file. Escape sequence is available (eg. '*.txt').
"""
# make output dir
os.makedirs(outdirpath, exist_ok=True)
# search target file
paths = glob.glob(trgfilename, recursive=True)
for path in paths:
dname = os.path.dirname(path).split('/')[-1]
fname = path.split('/')[-1]
shutil.copyfile(path, os.path.join(outdirpath,dname+'_'+fname))
if __name__ == '__main__':
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--outdir', type=str, required=True, help='path to output directory.')
parser.add_argument('--trgfile', type=str, required=True, help='name of target file.')
opt = parser.parse_args()
gatherfiles(opt.outdir, opt.trgfile)
I expect the newly-named files to be stored in an output file without being overwritten by each other.
Upvotes: 1
Views: 13095
Reputation: 67733
You're running this from the shell, which does its own glob expansion. If you want to pass the argument
./*/protein.faa
to your program un-expanded, you need to escape it to protect it from the shell, eg.
'./*/protein.faa'
Alternatively, as Taek said, you can change your function and arg parser to accept a pre-expanded list of target paths.
Upvotes: 1
Reputation: 1024
Your parser's argument --trgfile
takes one string.
But you call your script with a wildcard: —trgfile ./*/protein.faa
.
This wildcard is then evaluated to multiple strings hence the unrecognized argument error.
Either change the trgfile to accept multiple values (add nargs='+'
) or call your script with a single trgfile.
Upvotes: 1