passing wildcard arguments from bash into python -


i'm trying practice python script writing simple script take large series of files named a_b , write them location b\a. way passing arguments file

python script.py * 

and program looks

from sys import argv import os import ntpath import shutil  script, filename = argv  target = open(filename)  outfilename = target.name.split('_')   outpath=outfilename[1] outpath+="/" outpath+=outfilename[0]  if not os.path.exists(outfilename[1]):     os.makedirs(outfilename[1])  shutil.copyfile(target.name, outpath)  target.close() 

the problem this script way it's written set accept 1 file @ time. hoping wildcard pass 1 file @ time script execute script each time.

my question covers both cases:

  1. how instead pass wildcard files 1 @ time script.

and

  1. how modify script instead accept arguments? (i can handle list-ifying argv i'm having problems , im bit unsure how create list of files)

you have 2 options, both of involve loop.

to pass files 1 one, use shell loop:

for file in *; python script.py "$file"; done 

this invoke script once every file matching glob *.

to process multiple files in script, use loop there instead:

from sys import argv filename in argv[1:]:     # rest of script 

then call script bash python script.py * pass files arguments. argv[1:] array slice, returns list containing elements argv starting position 1 end of array.

i suggest latter approach means invoking 1 instance of script.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -