0

I am running a script in python like this from the prompt:

python gp.py /home/cdn/test.in..........

Inside the script i need to take the path of the input file test.in and the script should read and print from the file content. This is the code which was working fine. But the file path is hard coded in script. Now I want to call the path as a command line argument.

Working Script

             #!/usr/bin/python
             import sys
             inputfile='home/cdn/test.in'
             f = open (inputfile,"r")
             data = f.read()
             print data
              f.close()

Script Not Working

              #!/usr/bin/python
              import sys
              print "\n".join(sys.argv[1:])
              data = argv[1:].read()
              print data
              f.close()

What change do I need to make in this ?

OnesimusUnbound
  • 2,886
  • 3
  • 30
  • 40
Gopu
  • 1
  • 1
  • http://stackoverflow.com/questions/1009860/command-line-arguments-in-python check this related question. – pcalcao May 23 '13 at 10:21
  • in your non working script, what does this do `argv[1:].read()`? argv is a list of string, right? I think it has to be `data = open(sys.argv[1], "r").read()`. – OnesimusUnbound May 23 '13 at 10:25

3 Answers3

3

While Brandon's answer is a useful solution, the reason your code is not working also deserves explanation.

In short, a list of strings is not a file object. In your first script, you open a file and operate on that object (which is a file object.). But writing ['foo','bar'].read() does not make any kind of sense -- lists aren't read()able, nor are strings -- 'foo'.read() is clearly nonsense. It would be similar to just writing inputfile.read() in your first script.

To make things explicit, here is an example of getting all of the content from all of the files specified on the commandline. This does not use fileinput, so you can see exactly what actually happens.

# iterate over the filenames passed on the commandline
for filename in sys.argv[1:]:
    # open the file, assigning the file-object to the variable 'f'
    with open(filename, 'r') as f:
        # print the content of this file.
        print f.read()

# Done.
kampu
  • 1,391
  • 1
  • 10
  • 14
  • Nice answer, I bow to your calmness. I'm trying hard to be calm at explaining things that are **soo** clear to me but not others.. +1 – Niklas R May 23 '13 at 12:47
2

Check out the fileinput module: it interprets command line arguments as filenames and hands you the resulting data in a single step!

http://docs.python.org/2/library/fileinput.html

For example:

import fileinput
for line in fileinput.input():
    print line
Brandon Rhodes
  • 83,755
  • 16
  • 106
  • 147
0

In the script that isn't working for you, you are simply not opening the file before reading it. So change it to

          #!/usr/bin/python

          import sys

          print "\n".join(sys.argv[1:])
          f = open(argv[1:], "r")
          data = f.read()

          print data
          f.close()

Also, f.close() this would error out because f has not been defined. The above changes take care of it though.

BTW, you should use at least 3 chars long variable names according to the coding standards.

Amyth
  • 32,527
  • 26
  • 93
  • 135