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
Sad [Solved] I'm not getting the good type slain 18 494 Apr-11-2024, 01:36 PM
Last Post: slain
Question How to compare two parameters in a function that has *args? Milan 4 1,255 Mar-26-2023, 07:43 PM
Last Post: Milan
  [SOLVED] Input parameter: Single file or glob? Winfried 0 1,577 Sep-10-2021, 11:54 AM
Last Post: Winfried
  *args implementation and clarification about tuple status amjass12 10 3,994 Jul-07-2021, 10:29 AM
Last Post: amjass12
  Two Questions, *args and //= beginner721 8 3,477 Feb-01-2021, 09:11 AM
Last Post: buran
  does yield support variable args? Skaperen 0 1,664 Mar-03-2020, 02:44 AM
Last Post: Skaperen
  is there a way: repeat key word args Skaperen 2 2,226 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 2,720 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Using function *args to multiply multiple arguments allusernametaken 8 6,049 Nov-20-2019, 12:01 AM
Last Post: allusernametaken
  Passing string args to Popen CardBoy 3 4,238 Jan-16-2018, 09:22 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

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