发布时间:2019-09-20 07:34:44编辑:auto阅读(1589)
1、模拟cp命令拷贝文件(图片,视频)到其他路径下
with open('a.txt','rb') as f,open('b.txt','wb') as write: #选择rb模式,支持所有的拷贝,对应的写模式就是wb,直接处理的是bytes类型
for line in f: #遍历源文件的每一行
write.write(line) #写入新文件write中但是这样的没有支持用户自己传参数,所以最后的代码如下:
import sys
if len(sys.argv)<3: #用户输入参数少于3个
print('python3 copy.py source.file target.file ') #提示用户命令的用法需要3个参数
sys.exit()
with open(r'%s' %sys.argv[1],'rb') as f,open(r'%s' %sys.argv[2],'wb') as write: #这里处理的文件是基于用户输入的参数取到的
for line in f:
write.write(line)2、python模拟tail命令, 显示日志文件内容,不退出
python3 tail.py
import sys
import time
with open(r'%s' %sys.argv[2],'rb') as read:
read.seek(0,2) #2模式以末尾为参照,前面的0表示最后一个字节,保证光标直接在文件尾部
while True:
line=read.readline()
if line: #如果行有内容
print(line.decode('utf-8'),end='') #默认是字节码,转换为字符,end等于空表示换行符不单独打一行
else:
time.sleep(0.2) #休息0.2秒以后再接着做判断
上一篇: Python里调用redis的方法
下一篇: 关于Python的JSON
51117
50508
41125
37969
32436
29338
28208
23052
23020
21345
1381°
2077°
1730°
1659°
1948°
1733°
2405°
4060°
3939°
2814°