Python Forum
[SOLVED] Input parameter: Single file or glob?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[SOLVED] Input parameter: Single file or glob?
#1
Hello,

As input parameter, I need to allow users to give either a single file (input.txt) or glob (*.txt).

The following code only supports glob:
import sys import os from glob import glob #============= Read file(s) from command line; Windows and *nix shells don't behave the same way def argGlob(args=None):	if args is None: args = sys.argv	return [subglob for arg in args for subglob in glob(arg)] arguments = len(sys.argv) - 1 if arguments < 1:	print ("Must pass at least one TXT file, or *.txt")	sys.exit() for item in argGlob(sys.argv[1:]):
What would be the right way to handle both cases?

Thank you.

--
Edit: My mistake. I was using a wrong filename as input. The code above works to handle either a single file or a glob.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Linefeed when writing "f" strings to text file? Winfried 5 755 Nov-04-2025, 11:51 AM
Last Post: buran
Question [SOLVED] Open file, and insert space in string? Winfried 7 2,461 May-28-2025, 07:56 AM
Last Post: Winfried
Question [SOLVED] [Beautiful Soup] Replace tag.string from another file? Winfried 2 1,610 May-01-2025, 03:43 PM
Last Post: Winfried
Question [SOLVED] Same input different output antarling 2 1,518 Oct-25-2024, 11:28 PM
Last Post: antarling
  [SOLVED] [Linux] Write file and change owner? Winfried 6 3,132 Oct-17-2024, 01:15 AM
Last Post: Winfried
  [SOLVED] Reportlab - change a single page orientation beetlecad 1 4,539 Aug-16-2024, 05:43 PM
Last Post: beetlecad
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 4,038 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 16,279 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Loop through json file and reset values [SOLVED] AlphaInc 2 7,411 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 19,684 Dec-26-2022, 08:48 AM
Last Post: ibreeden

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.