欢迎光临!
若无相欠,怎会相见

Python学习之基础练习(3)

简介

继续学习,按照《可爱的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 ,其结果为:

如有错误,敬请指出,多谢

赞(0) 打赏
转载请注明:飘零博客 » Python学习之基础练习(3)
分享到: 更多 (0)

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

欢迎光临