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 ?

2
  • stackoverflow.com/questions/1009860/… check this related question. Commented May 23, 2013 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(). Commented May 23, 2013 at 10:25

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

1 Comment

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
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

1 Comment

Thx Brandon,OnesimusUnbound and Kampu. it worked...thanks a lot.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.