python:习题17

# -*- coding: cp936 -*-
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?

in_file = open (from_file)
indata = in_file.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()

out_file = open(to_file, ‘w‘)
out_file.write(indata)

#print "Alright, all done."

out_file.close()
in_file.close()

#改成一行
open(to_file, ‘w‘).write(open(from_file).read())

时间: 2024-12-05 11:19:23

python:习题17的相关文章

欧拉计划(python) problem 17

Number letter counts Problem 17 If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in w

【python】17个新手常见Python运行时错误

原文链接:http://www.oschina.net/question/89964_62779 想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的一些让你程序 crash 的运行时错误. 1)忘记在 if , elif , else , for , while , class ,def 声明末尾添加 :(导致 “SyntaxError :invalid syntax”) 该错误将发生在类似如下代码中: ? 1 2 if spam == 42     print('Hell

笨方法学Python——习题40

之前例子讲过关于字典.列表.字符串.元组,按书中所述,其实这些已经足够写一些代码,但Python属于面向对象的编程语言,本节所讲的类,是必须要掌握的,虽然现在有些懵逼 1 class Song(object): 2 3 def __init__(self, lyrics): 4 self.lyrics = lyrics 5 6 def sing_me_a_song(self): 7 for line in self.lyrics: 8 print line 9 10 happy_bday = S

python习题16

1 from sys import argv 2 3 script, filename = argv 4 5 print(f"We're going t o erase {filename}.") 6 print("If you don't want that, hit CTRL-(^C).") 7 print("If you do want that, hit RETURN.") 8 9 input("?") 10 11 p

python习题15

1 # 从python的内置模块中引入模组 2 from sys import argv 3 4 # 解包,获取文件名 5 script, filename = argv 6 7 #返回某文件对象 8 txt = open(filename) 9 10 #打印这句话,嵌入文件名 11 print(f"Here's your file {filename}:") 12 #读取文件内容并且打印 13 print(txt.read()) 14 15 print("Type the

笨办法学Python - 习题3: Numbers and Math

Exercise2是注释和井号 Comments and Pound Characters 具体详情请参考习题一,这里就不在做过多的赘述. 习题 3: 数字和数学计算 学习目标:了解Python中常用的算术运算符,并了解运算符之间的先后运算顺序 在各大常用的计算机语言中都有常见的算术运算符,Python也是大同小异,下面我们来了解一下Python中常见的算术运算符: 算术运算符 以下假设变量x = 10 ,y = 20 运算符 描述 实例 + 加 - 两个对象相加 x+y = 30 - 减 -

笨办法学Python - 习题8-10: Printing & Printing, Printing

目录 1.习题 8: 打印,打印 2.习题 9: 打印,打印,打印 3.习题 10: 那是什么? 3.1.转义序列: 4.习题总结: 1.习题 8: 打印,打印 学习目标:继续学习 %r 的格式化输出. 习题八中的练习代码是: #! -*-coding=utf-8 -*- formatter = "%r %r %r %r %r " print formatter % (1, "hello", [1,2,3], (1,2,3), {"name":&

python习题20190130

#encoding=utf-8 ''' 一家商场在降价促销.如果购买金额50-100元(包含50元和100元)之间,会给10%的折扣,如果购买金额大于100元会给20%折扣.编写一程序,询问购买价格,再显示出折扣(10%或20%)和最终价格 ''' def print_zekou(): aount = int(input("请输入你购买的金额:")) if aount < 50: print("不好意思你购买的金额没有折扣,需要支付的金额为:",aount)

python习题练习(chapater 5 -- python核心编程)

更新中... #!/usr/bin/env python# coding: utf-8'for practise in chapater five' #定义一个函数,计算并返回两个数的乘机def product(a, b): return(a * b) #根据分数输出同学的评分成绩(A-F)def score(i): if (i > 90) & (i < 100):  return('A') elif (i > 80) & (i < 89):  return('B'