Removing Features in the Script

A forum for general discussion of the Python programming language.

Removing Features in the Script

Postby HTHVampire » Thu May 09, 2013 9:23 am

Code: Select all
from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "Copying from %s to %s" % (from_file, to_file)

# We could do these two on one line too, how?
input = open(from_file)
indata = input.read()

print "The input file is %d bytes long" % len(indata)

print "Does the output file exists? %r" % exists(to_file)
print "Ready, hit RETURN to continue, CTRL-C to abort."
raw_input()

output = open(to_file, 'w')
output.write (indata)

print "Alright, all done."

output.close()
input.close()


From the above script, is it possible that I shortened them into just a line by removing the features? Thanks for your guides!
HTHVampire
 
Posts: 9
Joined: Sun Apr 14, 2013 1:31 pm

Re: Removing Features in the Script

Postby setrofim » Thu May 09, 2013 10:20 am

Sure. It's even possible to shorten it to zero lines by removing the features. Why do you want to do this?
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Removing Features in the Script

Postby HTHVampire » Thu May 09, 2013 12:18 pm

setrofim wrote:Sure. It's even possible to shorten it to zero lines by removing the features. Why do you want to do this?


The book that I read requested me to do so, and the author said that he can shortened into 1 line. I got no idea with it. Can you guide me how to do so? Thanks!
HTHVampire
 
Posts: 9
Joined: Sun Apr 14, 2013 1:31 pm

Re: Removing Features in the Script

Postby setrofim » Thu May 09, 2013 2:05 pm

Ah right, must be LPTHW. OK, keep in mind that this is just an exercise, and you would never actually do this in practice.
Here are a few pointers:
  • Get rid of the print statements, and the raw_input() line, as well as comments.
  • instead of importing argv from sys, you can just import sys, and then access argv as sys.argv.
  • sys.argv is just a list, so you can access invidual elements by indexing into it. E.g. from_file is sys.argv[1].
  • You can import multiple modules using a single import statement by separating module names with commas.
  • You can have multiple Python statements on one line by separating them with semicolons.
  • You don't need to store the result of calling a function in a variable, you can use it directly. E.g., you can say open(from_file).read().
  • You don't have to close file handles in this short script, as they will close automatically when the script exits (it's a really bad practice to rely on this, but for this exercises you'll have to).
  • You can copy files directly using functions in shutil module (not sure if the exercise means for you to use that or not).
Try applying the above and see how far you get. Post your attempt if you neeed more help.
setrofim
 
Posts: 288
Joined: Mon Mar 04, 2013 7:52 pm

Re: Removing Features in the Script

Postby HTHVampire » Fri May 10, 2013 3:10 am

setrofim wrote:[*]You don't need to store the result of calling a function in a variable, you can use it directly. E.g., you can say open(from_file).read().


Hi, thanks for your tips in my exercise. Yeah, the book is LPTHW. I hardly to understand the statement you said I quoted above. Can you give me more details?
This is the script that I have tried and tested :
Code: Select all
import sys ; script, from_file, to_file = sys.argv

input = open(from_file)
indata = input.read()

output = open(to_file, 'w')
output.write(indata)


The guides are useful to me. So, now I'm not sure how I can shortened the rest of it, I still need your further guides. Sorry, I'm really new in programming, so the extent of knowledge is very limited. Thanks!
HTHVampire
 
Posts: 9
Joined: Sun Apr 14, 2013 1:31 pm

Re: Removing Features in the Script

Postby Mekire » Fri May 10, 2013 4:51 am

He means you can do nonsense like this:
Code: Select all
open(to_file,'w').write(open(from_file).read())
which is also why he explicitly said:
setrofim wrote:keep in mind that this is just an exercise, and you would never actually do this in practice


-Mek

Edit: A little more. When you call a function, that function returns a value. You can assign this value to a name which is what you are accustomed to doing. Or you can apply methods to the return value directly.

open(myfile) returns a file so it is valid to write open(myfile).read()
You don't have to go through the step of storing its return value in a name.
User avatar
Mekire
 
Posts: 1120
Joined: Thu Feb 07, 2013 11:33 pm
Location: Asakusa, Japan

Re: Removing Features in the Script

Postby HTHVampire » Fri May 10, 2013 6:04 am

Hi Mek and Setrofim,

Thanks both of you guys help me to understand and try the exercise. I think I got an idea how to go on with it. Thanks!
HTHVampire
 
Posts: 9
Joined: Sun Apr 14, 2013 1:31 pm


Return to General Discussions

Who is online

Users browsing this forum: il_dottore and 3 guests