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] Same input different output antarling 2 828 Oct-25-2024, 11:28 PM
Last Post: antarling
  [SOLVED] [Linux] Write file and change owner? Winfried 6 1,451 Oct-17-2024, 01:15 AM
Last Post: Winfried
  [SOLVED] Reportlab - change a single page orientation beetlecad 1 1,357 Aug-16-2024, 05:43 PM
Last Post: beetlecad
  [solved] how to delete the 10 first lines of an ascii file paul18fr 7 1,639 Aug-07-2024, 08:18 PM
Last Post: Gribouillis
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 9,332 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Loop through json file and reset values [SOLVED] AlphaInc 2 5,133 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 17,221 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  python Multithreading on single file mg24 3 3,309 Nov-05-2022, 01:33 PM
Last Post: snippsat
  Create multiple/single csv file for each sql records mg24 6 2,745 Sep-29-2022, 08:06 AM
Last Post: buran
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 3,479 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy

Forum Jump:

User Panel Messages

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