简介
继续学习,按照《可爱的Python》一书,慢慢补充自己的不足之处。本次是基于Python 2.7.13开发
I/O操作
本次的文件I/O操作还是书上的题目。题目为:
读取文件 cdays−4-test.txt 内容,去除空行和注释行后,以行为单位进行排序,并将
结果输出为 cdays−4-result.txt。
cdays-4-test.txt内容如下:
#some words
Sometimes in life,
You find a special friend;
Someone who changes your life just by being part of it .
Someone who makes you laugh until you can' t stop;
Someone who makes you believe that there really is good in the world.
Someone who convinces you that there really is anunlocked door just waiting
for you to open it .
This is For ever Friendship.
when you're down,
and the world seems dark and empty,
Your forever friend lifts you up in spirits and makes that dark and empty
world
suddenly seem bright and full.
Your forever friend gets you through the hard times, the sad times, and the
confused times.
If you turn and walk away,
Your for ever friend follows,
If you lose you way,
Your forever friend guides you and cheers you on.
Your forever friend holds your hand and tells you that everything is going
to be okay.
但是在代码中,我就随便取的名字,代码如下:
fp = open("read1.txt", "r")
result = list()
for line in fp.readlines():
line = line.strip() # 去除空格
if not len(line) or line.startswith('#'): # 去除以#开头的行
continue
result.append(line)
# result.sort() # sort()函数能按照顺序排列
print result
open("readme1.txt", "w").write("%s" % "\n" .join(result))
结果如下
Sometimes in life,
You find a special friend;
Someone who changes your life just by being part of it .
Someone who makes you laugh until you can' t stop;
Someone who makes you believe that there really is good in the world.
Someone who convinces you that there really is anunlocked door just waiting
for you to open it .
This is For ever Friendship.
when you're down,
and the world seems dark and empty,
Your forever friend lifts you up in spirits and makes that dark and empty
world
suddenly seem bright and full.
Your forever friend gets you through the hard times, the sad times, and the
confused times.
If you turn and walk away,
Your for ever friend follows,
If you lose you way,
Your forever friend guides you and cheers you on.
Your forever friend holds your hand and tells you that everything is going
to be okay.
带参数的Python命令
本次的试验是书上的扫描文件目录信息的试验。此次,我进一步将功能稍微增强一些,既可以扫描当前目录的内容,还可以扫描指定路径的内容。请看源码:
import os
import sys
def dir_walker(cdPath, sfile):
export = ""
for root, dirs, files in os.walk(cdPath):
export += "%s; %s; %s; \n" % (root, dirs, files)
open(sfile, "w").write(export)
if "-save" ==sys.argv[1]:
dir_walker(os.path.dirname(os.path.abspath(__file__)), sys.argv[2])
print u"记录目录信息到 %s" % sys.argv[2]
elif "-path" == sys.argv[1]:
if "-save" == sys.argv[3]:
dir_walker(sys.argv[2], sys.argv[4])
print u"记录目录信息到 %s" % sys.argv[4]
else:
print u'''Pydir 使用方式:
python pydir.py -save filename
#将本脚本所在目录的内容记录为filename
python pydir.py -path dir -save filename
#将指定目录的内容记录为 filename
'''
扫描当前文件路径的信息方法为: python Pydir.py -save myfile1.txt
,结果如下:
扫描指定路径信息的方法是: python Pydir.py -path E:\Envs -save myfile.txt
,其结果为:
如有错误,敬请指出,多谢
最新评论
这个软件有bug的,客户端windows有些键不能用如逗号、句号
没有收到邮件通知
我的评论通知貌似坏掉了,定位一下问题
测试一下重新部署后的邮件功能
居然看到自己公司的MIB库,诚惶诚恐
那可能是RobotFramework-ride的版本问题。我装的1.7.4.2,有这个限制。我有空再尝试下旧版本吧,感谢回复。
你好!我在python2.7中安装RobotFramework-ride的时候提示wxPython的版本最高是2.18.12,用pip下载的wxPython版本是4.10,而且我在那个路径下没有找到2
真的太好了,太感谢了,在bilibili和CSDN上都找遍了,终于在你这里找到了