0

Possible Duplicate:
Command Line Arguments In Python

I am using Python 3.2. What I would like to do is basically a program that exports text into a .txt file, like this:

[program name] "Hello World" /home/marcappuccino/Documents/Hello.txt

I am a newbie and I dont know how to take whtever's in between the two ""s and place that into a variable. Is it in sys.argv? Any help is appreciated! Thanks.

Community
  • 1
  • 1
Marco Scannadinari
  • 1,774
  • 2
  • 15
  • 24

2 Answers2

3

Yes, it is sys.argv, which holds the command line arguments. You would want something like:

string_to_insert = sys.argv[1]
file_to_put_string_in = sys.argv[2]

This would assign "Hello World" to string_to_insert and /home/marcappuccino/Documents/Hello.txt to file_to_put_string_in.

Let's say you have a script named "dostuff.py" and you invoke it like this:

dostuff.py "Hello World 1" "Hello World 2" hello world three 

What you will end up with is:

sys.argv[0] = dostuff.py (might be a full path, depending on the OS)
sys.argv[1] = Hello World 1
sys.argv[2] = Hello World 2 
sys.argv[3] = hello 
sys.argv[4] = world 
sys.argv[5] = three 

Arguments in quotes are considered a single argument.

Smith
  • 361
  • 1
  • 10
  • so [1] and [2] are words seperated by spaces? if so then what if the usr put a string like this "Hello world" as opposed to "helloworld"? – Marco Scannadinari Oct 16 '12 at 21:52
  • Crummy formatting; see edited answer. – Smith Oct 16 '12 at 21:58
  • But i dont understand what the []s do,. How do they discern from [1] and [2] 3 etc? - how are they seperated? – Marco Scannadinari Oct 16 '12 at 22:01
  • If you don't understand what [] does, I would suggest you start from the very basics and do some reading about arrays/lists. – Smith Oct 16 '12 at 22:01
  • I understand, and i am in fact learning by creating this program. anyway i dont think your update came to me by the time i posted that comment. so i just want to understand when the []s end, for example, they might stop whenever there is a space or a symbol. – Marco Scannadinari Oct 16 '12 at 22:06
  • eg `program hello world lol` would be `sys.argv[1] == "hello"` `sys.argv[2] == "world"` and `sys.argv[3] == "lol"`..? – Marco Scannadinari Oct 16 '12 at 22:07
  • So, how does your example work then? sureley sys.argv[1] would be "Hello, and sys.argv[2] would be World, and sys.argv[3] would be 1"? – Marco Scannadinari Oct 16 '12 at 22:10
  • The arguments are split by spaces. The first element of sys.argv is always the filename (i.e. sys.argv[0]) and the second one is sys.argv[1] – ronak Oct 16 '12 at 22:11
  • the args are split by spaces, yes, but how are the [ ]s split? – Marco Scannadinari Oct 16 '12 at 22:12
  • It appears that slicing in python is that [1] selects the first character of the word and [2] selects the second etc, [1:2] selects everything in betwenn 1st and 2nd characters, but i cannot find out how to take anything between a character like " – Marco Scannadinari Oct 16 '12 at 22:29
  • so how do i do something like `usrtext = sys.argv["*"]` (the asterisk being an autocomplete for whatever is whitin two quotation marks)? – Marco Scannadinari Oct 16 '12 at 22:32
  • You don't. sys.argv is a list. List items are accessed via their index; i.e., sys.argv[0] is the first item in the list, sys.argv[1] is the second item in the list, etc. See: http://docs.python.org/library/stdtypes.html#typesseq – Smith Oct 16 '12 at 22:43
1

I wrote down a simple program to demonstrate what I believe you need. You have to add escape characters in the input to take the quotes as is.

import sys

for i in range(0, len(sys.argv)):
    print sys.argv[i]

Output:

python testing.py a b c "abcd"
testing.py
a
b
c
abcd

python testing.py a b c \"abcd\"
testing.py
a
b
c
"abcd"
ronak
  • 1,770
  • 3
  • 20
  • 34
  • thanks but, i just realised that argv was a list, and i now realise that answer 1 answered my question! if i could upvote i would but i am 1 rep... – Marco Scannadinari Oct 17 '12 at 15:32