python核心编程v2.0 第三章习题答案
来源:互联网 发布:如何修改mac锁屏界面 编辑:程序博客网 时间:2024/05/18 01:16
工具:pycharm
3.10
#coding=utf-8import osls = os.linesepwhile True : filename = raw_input('Filename :\n') # wronly : 只写模式 creat:创建新文件 excl 若文件已存在则返回错误 try: fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_EXCL) except OSError, e : print "already has the file\n" else: breakall = ''while True : entry = raw_input('>') if entry == '.' : all = all + '.' break else: all = all + entry all = all +' 'os.write(fd,all)os.close(fd)
import oswhile True : fname = raw_input('Filename = :\n') if os.path.exists(fname) : fobj = open(fname,'r') for eachlines in fobj : print eachlines, fobj.close() break else: print "no such file"
3.11
import oswhile True : fname = raw_input('Filename = :\n') if os.path.exists(fname) : fobj = open(fname,'r') for eachlines in fobj : #去除每行最后的换行符 eachlines = eachlines.strip('\n') print eachlines fobj.close() break else: print "no such file"
3.12
#coding=utf-8import osdef makefile() : ls = os.linesep while True : filename = raw_input('Filename :\n') # wronly : 只写模式 creat:创建新文件 excl 若文件已存在则返回错误 try: fd = os.open(filename, os.O_WRONLY | os.O_CREAT | os.O_EXCL) except OSError, e : print "already has the file\n" else: break all = '' while True : entry = raw_input('>') if entry == '.' : all = all + '.' break else: all = all + entry all = all +' ' os.write(fd,all) os.close(fd)def readfile() : while True : fname = raw_input('Filename = :\n') if os.path.exists(fname) : fobj = open(fname,'r') for eachlines in fobj : eachlines = eachlines.strip('\n') print eachlines fobj.close() break else: print "no such file"if __name__ == '__main__' : while True : index = raw_input('read=r,write=w,quit=q:\n') if index == 'w': makefile() elif index == 'r' : readfile() elif index == 'q' : break
3.13
参考网上的代码,只加了注释其余未改动
Tkinter模块:GUI编程,用于创建图形化窗口
platform模块:用于获得操作系统,系统结构,python信息等
#!/usr/bin/env python#-*- coding: utf-8 -*-#=============================================================================# FileName:# Desc:# Author: ToughGuy# Email: wj0630@gmail.com# HomePage: http://www.techzhai.net/# Version: 0.0.1# LastChange: 2013-02-20 14:52:11# History:#=============================================================================from Tkinter import *import tkMessageBox,tkFileDialogimport platform# nl = os.linesep#从系统中打开文件,将内容读在edit.box中def openfile(): global filename # 使用global声明为全局变量,方便后边的程序调用 systype = platform.system() # 判断系统类型 if systype == 'windows': basedir = 'c:\\' else: basedir = '/' filename = tkFileDialog.askopenfilename(initialdir=basedir) try: fobj_r = open(filename, 'r') except IOError, errmsg: print '*** Failed open file:', errmsg else: editbox.delete(1.0, END) for eachline in fobj_r: editbox.insert(INSERT, eachline) fobj_r.close()#将editbox中的内容保存在当前文件中def savefile(): save_data = editbox.get(1.0, END) try: fobj_w = open(filename, 'w') fobj_w.writelines(save_data.encode('utf-8')) # 感谢OSC-骠骑将军 指教 fobj_w.close() tkMessageBox.showinfo(title='提示', message='保存成功') except IOError, errmsg: tkMessageBox.showwarning(title='保存失败', message='保存出错 ') tkMessageBox.showwarning(title='错误信息', message=errmsg) except NameError: tkMessageBox.showwarning(title='保存失败', message='未打开文件')def showlinenum(): tkMessageBox.showinfo(title='提示', message='这个功能作者现在不会写,放这里装饰用的.')def destroy_ui(ui): ui.destroy()def aboutauthor(): author_ui = Toplevel() author_ui.title('关于') author_ui.geometry('200x80') about_string = Label(author_ui, text="作者: ToughGuy\n\n主页: http://www.techzhai.net/") confirmbtn = Button(author_ui, text='确定', command=lambda:destroy_ui(author_ui)) about_string.pack() confirmbtn.pack() # author_ui.mainloop()#创建框架def CreateMenus(): # 初始化菜单 Menubar = Menu(root) # 创建文件菜单 filemenu = Menu(Menubar, tearoff=0) filemenu.add_command(label='打开文件', command=openfile) filemenu.add_command(label='保存文件', command=savefile) filemenu.add_command(label='退出', command=lambda:destroy_ui(root)) Menubar.add_cascade(label='文件', menu=filemenu) # 创建编辑菜单 editmenu = Menu(Menubar, tearoff=0) editmenu.add_command(label='显示行号', command=showlinenum) Menubar.add_cascade(label='编辑', menu=editmenu) # 创建帮助菜单 helpmenu = Menu(Menubar, tearoff=0) helpmenu.add_command(label='关于作者', command=aboutauthor) Menubar.add_cascade(label='帮助', menu=helpmenu) root.config(menu=Menubar)root = Tk()root.title('文本编辑器')root.geometry('500x400')CreateMenus()#创建编辑区域editbox = Text(root, width=70, height=25, bg='white')editbox.pack(side=TOP, fill=X)root.mainloop()
阅读全文
0 0
- python核心编程v2.0 第三章习题答案
- Python核心编程v2.0 第二章习题答案
- python核心编程v2.0 第四章习题答案
- python核心编程v2.0 第五章习题答案
- python核心编程v2.0 第六章习题答案
- python核心编程v2.0 第8章习题答案
- Python核心编程v2.0 第10章习题答案
- Python核心编程v2.0 第11章习题答案
- Python核心编程v2.0 第12章习题答案
- python核心编程v2.0 第14章习题答案
- python核心编程v2.0 第7章习题答案(下)
- python核心编程v2.0 第9章习题答案(上)
- Python核心编程v2.0 第9章习题答案(下)
- python核心编程-第六章习题答案
- 《Python 核心编程》 第三章习题 3.10
- python核心编程第七章习题答案(2)
- python核心编程第七章习题答案(3)
- python核心编程第八章习题答案(1)
- sqlserver 命令行的使用
- java ip排序实例
- 线性表之栈的链式存储实现
- HDOJ 1059 Dividing(多重背包+二进制优化)
- 我的网站收集
- python核心编程v2.0 第三章习题答案
- Java语言基础(七)
- 各种安装的记录
- 大学微积分1----Taylor中值定理和麦克劳林公式-----1
- eval
- 因子平方和(python实现)
- UGUI自适应笔记
- TCP的三次握手四次挥手
- hibernate的反向生成entity出现一个abstract