python 学习源码练习(2)——简单文件读取
#文件创建
#!/usr/bin/python3
'makeTextFile.py--create text file'
import os
ls = os.linesep
#get filename
fname = input('please enter the filename:')
while True:
if os.path.exists(fname):
print ( "Error : '%s' already exists" % fname)
else:
break
#get file content(text) lines
all = []
print ("\nEnter Lines('.' by itself to quit).\n")
#loop until user terminates input
while True:
entery = input('>')
if entery == '.':
break
else:
all.append(entery)
#wirte lines in file
fobj = open(fname, 'w')
fobj.writelines(['%s%s' %(x, ls) for x in all])
fobj.close()
print ('Done!')
#read file and output to the screen
#!/usr/bin/python3
'readTextFile.py--read and display text file'
#get file name
fname = input('please input the file to read:')
try:
fobj = open(fname,'r')
except:
print("*** file open error" ,e)
else:
#display the contents of the file to the screen.
for eachline in fobj:
print(eachline,)
fobj.close()
python 学习源码练习(2)——简单文件读取的更多相关文章
- 通过阅读python subprocess源码尝试实现非阻塞读取stdout以及非阻塞wait
http://blog.chinaunix.net/uid-23504396-id-4661783.html 执行subprocess的时候,执行不是问题最麻烦的是获取进程执行后的回显来确认是否正确执 ...
- python学习笔记~INI、REG文件读取函数(自动修复)
引入configparser,直接read整个INI文件,再调用get即可.但需要注意的是,如果INI文件本身不太规范,就会报各种错,而这又常常不可避免的.本文自定义函数通过try...except. ...
- 【Spring源码分析】.properties文件读取及占位符${...}替换源码解析
前言 我们在开发中常遇到一种场景,Bean里面有一些参数是比较固定的,这种时候通常会采用配置的方式,将这些参数配置在.properties文件中,然后在Bean实例化的时候通过Spring将这些.pr ...
- python 学习源码练习(1)
#编译方式,python3 文件名 #!/usr/bin/python3#print('hello world') mystring = 'hello world'print (mystring) # ...
- python学习笔记6-输入输出与文件读取写入
(1)打印到屏幕:print (2)读取键盘输入:input/raw_input #键盘输入 str = raw_input("Please enter:"); print (&q ...
- 《python解释器源码剖析》第0章--python的架构与编译python
本系列是以陈儒先生的<python源码剖析>为学习素材,所记录的学习内容.不同的是陈儒先生的<python源码剖析>所剖析的是python2.5,本系列对应的是python3. ...
- NLP大赛冠军总结:300万知乎多标签文本分类任务(附深度学习源码)
NLP大赛冠军总结:300万知乎多标签文本分类任务(附深度学习源码) 七月,酷暑难耐,认识的几位同学参加知乎看山杯,均取得不错的排名.当时天池AI医疗大赛初赛结束,官方正在为复赛进行平台调 ...
- 【转】python:让源码更安全之将py编译成so
python:让源码更安全之将py编译成so 应用场景 Python是一种面向对象的解释型计算机程序设计语言,具有丰富和强大的库,使用其开发产品快速高效. python的解释特性是将py编译为独有的二 ...
- VS2015 Git 源码管理工具简单入门
1.VS Git插件 1.1 环境 VS2015+GitLab 1.2 Git操作过程图解 1.3 常见名词解释 拉取(Pull):将远程版本库合并到本地版本库,相当于(Fetch+Meger) 获取 ...
随机推荐
- UVA - 10249 The Grand Dinner
Description Problem D The Grand Dinner Input: standard input Output: standard output Time Limit: 15 ...
- Office Web Add-in的技术原理和开发常见问题剖析
作者:陈希章 发表于 2017年12月20日 我过去发表过一些Office Add-in开发的文章,并且也在不同的场合分享过新的开发模式及其带来的机遇.有不少朋友给我反馈,也讨论到一些常见问题,我这里 ...
- Linux常用操作命令及快捷键
Linux操作命令: 大体分为两类: 1.内部命令 help 命令(查看内部命令) 2.外部命令 命令 --help(查看外部命令) type 命令:用来查看该命令是内部命令还是外 ...
- CNN中的卷积操作的参数数计算
之前一直以为卷积是二维的操作,而到今天才发现卷积其实是在volume上的卷积.比如输入的数据是channels*height*width(3*10*10),我们定义一个核函数大小为3*3,则输出是8* ...
- iOS Button 上文字图片位置的设置
1. 添加图片+文字/文字+图片 ,不分前后,图片默认在文字前边 加空格隔开 UIButton * button =[[UIButton alloc] initWithFrame:CGRectMake ...
- go实例之函数
1.可变参数 示例代码如下: package main import "fmt" // Here's a function that will take an arbitrary ...
- Udacity并行计算课程笔记-The GPU Programming Model
一.传统的提高计算速度的方法 faster clocks (设置更快的时钟) more work over per clock cycle(每个时钟周期做更多的工作) more processors( ...
- 669. Trim a Binary Search Tree
Given a binary search tree and the lowest and highest boundaries as `L`and `R`, trim the tree so t ...
- bzoj 1597: [Usaco2008 Mar]土地购买
Description 农 夫John准备扩大他的农场,他正在考虑N (1 <= N <= 50,000) 块长方形的土地. 每块土地的长宽满足(1 <= 宽 <= 1,000 ...
- rmdir 命令详解
rmdir 作用: 用来删除空目录, 当目录不再被使用时, 或者磁盘空间已达到使用限定值, 就需要删除失去价值的目录. 利用rmdir 命令可以从一个目录中删除一个或多个空的子目录. 该命令从一个 ...