1 文件进度条
代码需求:
实现可视化, 不断增加 ##### 的功能
代码实现:
- #!/user/bin/env ptyhon
- # -*- coding:utf-8 -*-
- # Author: VisonWong
- import sys,time
- for i in range(20):
- sys.stdout.write("#")
- sys.stdout.flush()
- time.sleep(0.2)
输出结果:
E:\Python\PythonLearing\venv\Scripts\python.exe E:/Python/PythonLearing / 进度条. py
####################
会有不断增加 #的动画效果
2 文件内容参数替换
代码需求:
通过运行相应方法, 输入替换内容和被替换内容, 实现文件内容的替换
- #!/user/bin/env ptyhon
- # -*- coding:utf-8 -*-
- # Author: VisonWong
- from sys import argv
- script,find_str,replace_str = argv
- f = open(yesterday.txt,r,encoding=utf-8)
- f1 = open(yesterday_bak.txt,w,encoding=utf-8)
- for line in f:
- if find_str in line:
- line = line.replace(find_str,replace_str)
- f1.write(line)
- f.close()
- f1.close()
输出结果:
运行时, 在命令行输入:
python changefile.py young old
原文件内容:
Oh, yesterday when I was young
噢 昨日当我年少轻狂
So many, many songs were waiting to be sung
有那么那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
There are so many songs in me that wont be sung
我有太多歌曲永远不会被唱起
I feel the bitter taste of tears upon my tongue
我尝到了舌尖泪水的苦涩滋味
The time has come for me to pay for yesterday
终于到了付出代价的时间 为了昨日
When I was young
当我年少轻狂
修改后文件内容:
Oh, yesterday when I was old
噢 昨日当我年少轻狂
So many, many songs were waiting to be sung
有那么那么多甜美的曲儿等我歌唱
So many wild pleasures lay in store for me
有那么多肆意的快乐等我享受
And so much pain my eyes refused to see
还有那么多痛苦 我的双眼却视而不见
There are so many songs in me that wont be sung
我有太多歌曲永远不会被唱起
I feel the bitter taste of tears upon my tongue
我尝到了舌尖泪水的苦涩滋味
The time has come for me to pay for yesterday
终于到了付出代价的时间 为了昨日
When I was old
当我年少轻狂
来源: http://www.bubuko.com/infodetail-2545899.html