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] Correct way to convert file from cp-1252 to utf-8? Winfried 8 546 Feb-29-2024, 12:30 AM
Last Post: Winfried
  Loop through json file and reset values [SOLVED] AlphaInc 2 1,962 Apr-06-2023, 11:15 AM
Last Post: AlphaInc
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,659 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  python Multithreading on single file mg24 3 1,672 Nov-05-2022, 01:33 PM
Last Post: snippsat
  Create multiple/single csv file for each sql records mg24 6 1,323 Sep-29-2022, 08:06 AM
Last Post: buran
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 1,105 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
  How to split the input taken from user into a single character? mHosseinDS86 3 1,138 Aug-17-2022, 12:43 PM
Last Post: Pedroski55
  Reading an Input File DaveG 1 1,214 Mar-27-2022, 02:08 AM
Last Post: deanhystad
  function with 'self' input parameter errors out with and without 'self' called dford 12 3,009 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  Updating a config file [solved] ebolisa 8 2,532 Nov-04-2021, 10:20 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