% cat in
this is the contents of in
% cat test
this is the contents of test
% python3 cat.py “in” “test”
this is the contents of in
this is the contents of test
需要编写的是cat.py程序,程序是在命令行运行的
linux小白求助:如何编写python程序模拟linux的cat命令?题目具体如下。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答
- weixin_39195527 2017-09-01 06:05关注
首先我们先认识一下cat指令的主要功能,如果不加任何参数,cat $file, 就是将文件内容输出到标准输出。
如果我们所要写的python脚本就是为了实现这一功能,那么就很好办了,以下是样例代码:
import re import sys import os def main(): """ """ file_list = sys.argv[1:] for file_name in file_list: if os.path.exists(file_name): with open(file_name) as file_obj: print file_obj.read() else: print "File %s doesn't exist!" % file_name if __name__ == "__main__": reload(sys) sys.setdefaultencoding("utf-8") main()
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 MATLAB动图的问题
- ¥15 求差集那个函数有问题,有无佬可以解决
- ¥15 【提问】基于Invest的水源涵养
- ¥20 微信网友居然可以通过vx号找到我绑的手机号
- ¥15 寻一个支付宝扫码远程授权登录的软件助手app
- ¥15 解riccati方程组
- ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
- ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
- ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
- ¥50 树莓派安卓APK系统签名