Python Forum
[SOLVED] Good way to handle input args?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Good way to handle input args?
#1
Hello,

My script could be called with either discrete filenames or a glob:

#myscript.py file1.txt file2.txt
#myscript.py *.txt

for item in sys.argv[1:]:
for file in glob.glob(sys.argv[1]):
Before I hack something, do you know of a good way to handle both cases?

Thank you.
Reply
#2
Filenames glob to themselves. If you want to accept globs, I would just pass them all through glob. If one is just a filename, you'll get that filename back. I'd also use itertools to put it into one single list.

import itertools
import glob
filenames = list(itertools.chain.from_iterable([glob.glob(x) for x in sys.argv[1:]]))
Winfried likes this post
Reply
#3
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  *args implementation and clarification about tuple status amjass12 10 404 Jul-07-2021, 10:29 AM
Last Post: amjass12
  Two Questions, *args and //= beginner721 8 754 Feb-01-2021, 09:11 AM
Last Post: buran
  does yield support variable args? Skaperen 0 650 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 764 Feb-03-2020, 06:03 PM
Last Post: Skaperen
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 1,034 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Using function *args to multiply multiple arguments allusernametaken 8 1,937 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  Passing string args to Popen CardBoy 3 2,569 Jan-16-2018, 09:22 AM
Last Post: Gribouillis
  subprocess with args haye 0 3,028 Oct-23-2017, 10:54 AM
Last Post: haye
  how to use *args haye 7 3,187 Sep-25-2017, 01:21 PM
Last Post: haye
  How to pass args to myprog.exe Fran_3 3 6,203 Sep-05-2017, 12:51 AM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020