Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8270171
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T06:29:52+00:00 2026-06-08T06:29:52+00:00

I’m reading Zed Shaw’s Learn Python The Hard Way. I’m up to exercise 17

  • 0

I’m reading Zed Shaw’s “Learn Python The Hard Way”. I’m up to exercise 17 ( http://learnpythonthehardway.org/book/ex17.html ) and hit the wall on extra credit #’s 2 & 3. Zed wants me to shorten the script by eliminating anything that isn’t necessary (he claims he can get it to run with just one line in the script).

Here is the original script…

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 exist? %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()

Here’s what I was able to shorten the script to and still get it to run properly (by properly I mean that the script successfully copies the intended text to the intended file)…

from sys import argv
from os.path import exists

script, from_file, to_file = argv

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

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

I got rid of the print commands and the two close commands (please excuse if I’m using “command” incorrectly…I’m painfully new to coding and haven’t gotten the jargon down yet).

Anything else I try to further shorten the script produces errors. For example, I tried to combine the “input” and “indata” commands into one line like so…

input = open (from_file, 'r')

Then I changed any “indata” references in the script to “input”…

from sys import argv
from os.path import exists

script, from_file, to_file = argv

input = open (from_file, 'r')

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

But I get the following TypeError…

new-host:python Eddie$ python ex17.py text.txt copied.txt
Traceback (most recent call last):
  File "ex17.py", line 10, in <module>
    output.write (input)
TypeError: expected a character buffer object

How would you go about shortening the script further…or shortening it down to just one line, as Zed suggests he can do?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T06:29:56+00:00Added an answer on June 8, 2026 at 6:29 am
    from sys import argv
    open(argv[2], 'w').write(open(argv[1]).read())
    

    is about as short as you can get that. You could use a semicolon to join them into one line, but that’s just replacing a end-of-line character with something else and not really useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I would like my Web page http://www.gmarks.org/math_in_e-mail.txt on my Apache 2.2.14 server to display
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.