1

I'm current working through Learning Python the Hard Way, and it is perhaps going a little fast for me. I've input the following code, with along with the corresponding file. In the py file, I wrote:

#!/usr/bin/python

from sys import argv

script, filename = argv

txt = open(filename)

print "Here's your file %r:" % filename
print txt.read()

print "Type the filename again:"
file_again = raw_input("> ")

txt_again = open(file_again)

print txt_again.read()

And to run it, I wrote: python script.py readme.txt which ran the code.

However, I don't quite understand the process here:

  • How come #!/usr/bin/python must be at the top of the file
  • What is sys import argv
  • What is script, filename = argv
  • Is .read() a built in function?
glglgl
  • 89,107
  • 13
  • 149
  • 217

4 Answers4

3
  1. #!/usr/bin/python is the so-called Shebang, a hint to the OS kernel that it is a Python script which should be executed with the given Python binary. Thus, if you chmod +x the script, you can even call it with ./script.py readme.txt.

  2. from sys import argv is a command to import argv from the sys module directly into our namespace. So we can use argv to access it instead of sys.argv. If we only use it once, it may be better to just import sys and access everything inside via e.g. sys.argv. You'll find about that in the Python docs and/or tutorial.

  3. script, filename = argv is a shorthand for

    script = argv[0]
    filename = argv[1]
    

    as long as argv contains exactly 2 elements. You'll find about that in the Python docs and/or tutorial.

  4. file.read() is indeed built-in, but as a file object method, not as a function as such.

glglgl
  • 89,107
  • 13
  • 149
  • 217
2

Answers:

1) #!/usr/bin/python is there for UNIX users, it shows python where to find certain files. (Why do people write #!/usr/bin/env python on the first line of a Python script?)

2) sys import argv is the file in the argument [readme.txt] (http://www.tutorialspoint.com/python/python_command_line_arguments.htm)

3) script, filename = argv Script and Filename [new variables] take on the value from argv.

4) Yes, .read() is a built in function. (http://www.diveintopython.net/file_handling/file_objects.html)

Google is your friend on this one...

Community
  • 1
  • 1
  • I can't seem to find `read()` built-in function here: http://docs.python.org/2/library/functions.html#built-in-functions – Ashwini Chaudhary Aug 04 '13 at 21:49
  • Hmm, odd. I gave a source, that said it was, though... – Static Cast Aug 04 '13 at 21:51
  • From your source: "The `read` **method** reads a specified number of bytes", there's no built-in function mentioned there. [`read` is method of file objects](http://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects). – Ashwini Chaudhary Aug 04 '13 at 21:59
  • Alright, I see what you mean. Even though it is a method of file objects, It still is a built-in method (for file objects). – Static Cast Aug 05 '13 at 00:34
2

1) Unix-like convention. It allows to determine what to do with the file as soon as the first line of file is read.

2) from sys import argv means import only argv from the module named sys

3) a,b = 1,2 assigns 1 to a, and 2 to b. If there is an _iterable_ object on the RHS then such constructs are equivalent:

script, filename = argv 

===

script = argv[0]
filename = argv[1]

4) Yes. Maybe not built-in strictly speaking - it's just a function provided in one of Python's standard input output module.

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
2

The #!/usr/bin/python will turn your python script into an executable, a special unix trick. The executable scripts usually start with a line that begins with thecharacters #! (“hash bang”), followed by the path to the Python interpreter on your machine. Then by changing the mode to +x of that particular script file, you will be able to execute it on the prompt by just calling the name. As this path may be different for different machines, you can use the unix "env" workaround: #!/usr/bin/env python This will allow the env program to locate the python interpreter and execute the code

From modulename import something is used to import a module, but we are only importing "argv" attribute from the module sys. From is just like import, but does a bit more work for you where it copies the module's attribute (argv) in this case, so that they become simple variables in the recipient script.

script, filename = argv will simply assign argv[0] and argv[1] to script and filename respectively.

And finally, file.read is a builtin method for file objects.

Amit
  • 482
  • 4
  • 10
  • 1
    +1, but the she-bang will not turn the script into an executable, strictly speaking. It is one of the two prequisites for doing so, as you write later on. But I don't know a better wording, alas... – glglgl Aug 05 '13 at 06:29
  • You are right, the extra step of changing the script mode to +x is also required to make it executable.. I should have rather said it's the first step in making the script executable. Thanks for the comment! – Amit Aug 05 '13 at 13:41