Python Forum
How to iterate over command line arguments?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to iterate over command line arguments?
#1
How do you iterate over arguments passed to a script? I'm trying to use sys.argv as shown below, which does not work.

-Thanks


print 'Number of arguments:', len(sys.argv), 'arguments.'
print 'Argument List:', str(sys.argv)

for jpgfile in str(sys.argv):
    print(jpgfile)
Reply
#2
How does it not work? It works for me, although note that looping over a string loops over each character in the string. You did import sys, right? It's not in your code.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
(Nov-20-2018, 03:20 AM)ichabod801 Wrote: How does it not work? It works for me, although note that looping over a string loops over each character in the string. You did import sys, right? It's not in your code.

Here is what I got for output. How do I print each individual argument?

Output:
im-datestamp.py aaa bbb Number of arguments: 3 arguments. Argument List: ['./im-datestamp.py', 'aaa', 'bbb'] [ ' . / i m - d a t e s t a m p . p y ' , ' a a a ' , ' b b b ' ]
Reply
#4
>>> type(sys.argv)
<class 'list'>
Iterating over a string returns each letter. for element in sys.argv: should do it.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Nov-20-2018, 07:01 AM)wavic Wrote:
>>> type(sys.argv)
<class 'list'>
Iterating over a string returns each letter. for element in sys.argv: should do it.

Thanks, I wound up using this which eliminates the script name in [0]:

for jpgfile in sys.argv[1:]:
Reply
#6
Exactly!

Look at the argparse module. It allows much more flexibility and control over the arguments.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Accessing varying command line arguements Rakshan 3 172 Jul-28-2021, 03:18 PM
Last Post: snippsat
  How to input & output parameters from command line argument shantanu97 1 556 Apr-13-2021, 02:12 PM
Last Post: Larz60+
  Passing List of Objects in Command Line Python usman 7 999 Sep-27-2020, 03:45 PM
Last Post: ndc85430
  Taking Multiple Command Line Argument Input bwdu 6 1,450 Mar-29-2020, 05:52 PM
Last Post: buran
  python 3 from command line Dixon 1 732 Mar-01-2020, 08:35 PM
Last Post: snippsat
  execute linux command with arguments sivareddy 2 705 Feb-10-2020, 03:23 PM
Last Post: sivareddy
  Running linux command line apps... dbrdh 0 687 Jan-30-2020, 01:14 PM
Last Post: dbrdh
  command line input (arg parse) and data exchange Simba 7 1,835 Dec-06-2019, 11:58 PM
Last Post: Simba
  code works at command line but not in .py ridgerunnersjw 4 1,016 Sep-12-2019, 11:09 PM
Last Post: ridgerunnersjw
  changing the process command line arguments in python Skaperen 3 1,243 Aug-19-2019, 02:40 PM
Last Post: wavic

Forum Jump:

User Panel Messages

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