LPTHW 笨方法学习python 16章
生活随笔
收集整理的這篇文章主要介紹了
LPTHW 笨方法学习python 16章
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
根據(jù)16章的內容作了一些擴展。
?
比如,判斷文件如果存在,就在文件后追加,如不存在則創(chuàng)建。
同時借鑒了shell命令中類似 cat <<EOF > test的方法,提示用戶輸入一個結尾符。
現(xiàn)在有一個小坑,怎么使用python去讀取一個文件的行數(shù),原來有os.system("wc -l filename")倒是可以,但是windows下如何操作呢?回頭補填。
?
#!/usr/bin/env python # -*- coding:utf-8 -*- from sys import argv from os import path script, filename = argv if path.exists(filename):print "We're going to erase %r." % filenamet_module = "a" else:print "We will create %r." % filenamet_module = "w" end_EOF = raw_input("please give me a EOF string") with open(filename,t_module) as fo:line_count = 1while True:line_content = raw_input("please input %d line string:\n" % line_count)if line_content == end_EOF:breakelse:fo.write(line_content)fo.write("\n")
轉載于:https://www.cnblogs.com/sageskr/p/4075258.html
總結
以上是生活随笔為你收集整理的LPTHW 笨方法学习python 16章的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 进度条控制(Windows 公共进度栏控
- 下一篇: 初识Python(1)__Python基