calelin 2017-09-01 03:42 采纳率: 0%
浏览 1299
已采纳

linux小白求助:如何编写python程序模拟linux的cat命令?题目具体如下。

%​ ​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程序,程序是在命令行运行的

  • 写回答

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()
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘