首页 > 系统相关 >Python:模拟linux命令cat

Python:模拟linux命令cat

时间:2023-05-26 15:36:32浏览次数:37  
标签:option Python argv cat sys version linux print filename


模拟linux的cat命令,打印从命令行输入的文件名

#!/usr/bin/python
#Filename:cat.py

helpString = '''\
This program prints files to the standard output.
Any number of files can be specified.
Options include:
--version : Prints the version number
--help : Display this help'''

import sys

def readfile(filename):
    '''Print a file to the standard output.'''
    f = file(filename)
    while True:
        line = f.readline()
        if len(line) == 0:
            break
        print line,
    f.close()

#Script start from here
if len(sys.argv) < 2:
    print 'No action specified.'
    sys.exit()

if sys.argv[1].startswith('--'):
    option = sys.argv[1][2:]
    if option == 'version' or option == 'v':
        print 'Version 1.2'
    elif option == 'help' or option == 'h':
        print helpString
    else:
        print 'Unknown option.'
    sys.exit()
else:
    for filename in sys.argv[1:]:
        readfile(filename)

print 'Done'




标签:option,Python,argv,cat,sys,version,linux,print,filename
From: https://blog.51cto.com/u_16131207/6356771

相关文章

  • Python:使用cPickle储存器存储对象
    一个简单的例子,演示了怎么使用cPickle存储对象#!/usr/bin/python#Filename:pickling.pyimportcPickleaspshoplistfile='shoplist.data'shoplist=['apple','mango','carrot']f=file(shoplistfile,'w')p.dump(shoplist,......
  • python中的exec()函数的作用
    exec语句用来执行储存在字符串或文件中的Python语句。例如,我们可以在运行时生成一个包含Python代码的字符串,然后使用exec语句执行这些语句。下面是一个简单的例子。>>>exec'print"HelloWorld"'HelloWorld注意例子中exec语句的用法和eval_r(),execfile()是不一样的.exec......
  • linux的父进程向子进程发kill信号例子以及对子进程的状态进行判断
    先看一个父进程向子进程发kill信号例子:#include<stdio.h>#include<unistd.h>#include<signal.h>#include<sys/types.h>#include<sys/wait.h>intmain(intargc,constchar*argv[]){pid_tpid;intstatus;pid=fork();if(......
  • 配置 linux 的 bash 忽略命令大小写 和 能根据输入的命令头按“上“键显示该命令的历
    一般情况下,bash对命令是区分大小写的,当按“上键”时,能够显示前一个历史命令。但是,这还不太方便,下面的这个配置,可以让bash忽略大小写,而且,能根据输入的命令头按“上“键显示该命令的历史,比如:输入vim,然后按”上“键,此时,可以显示上一次运行vim时的那条命令,非常的方便!下面是该配置的......
  • 提高linux对最大进程数和最大打开文件描述符数的限制
    打开/etc/security/limits.conf文件在下面加入如下两行,其中wacos是用户名,*可以代表所有用户wacos     -   nproc     20000wacos     -   nofile     65536noproc代表最大进程数nofile代表最大文件打开数然后在命令行输......
  • linux shell中 test 的用法
    1)判断表达式 iftest (表达式为真) iftest!表达式为假 test表达式1–a表达式2                 两个表达式都为真 test表达式1–o表达式2                两个表达式有一个为真 2)判断字符串 test–n字符串   ......
  • linux的文件类型
    在介绍属性时,提到了最前面的标志(d或-)可以表示目录或文件,那就是不同的文件种类。Linux的文件种类主要有下面这几种:•普通文件(regularfile):就是一般我们存取的文件,由ls-al显示出来的属性中,第一个属性为[-],例如[-rwxrwxrwx]。另外,依照文件的内容,又大致可以分为:• 纯文本文件(A......
  • 编译安装python3.11.3
     1、下载源码包cd/usr/local/srcwget'https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tar.xz' 2、解压xz-dPython-3.11.3.tar.xztarzfPython-3.11.3.tar 3、安装编译工具和依赖包yum-yinstallgcc-c++openssl-devel 4、配置mkdir-p/......
  • Educational Codeforces Round 63 (Rated for Div. 2) A,B,C
    A.ReverseaSubstring传送门就是找不满足升序排列的字母,输出就行了。#include<bits/stdc++.h>#definelllonglongusingnamespacestd;constintmaxn=3e5+10;chars[maxn];intmain(){#ifndefONLINE_JUDGEfreopen("in","r",stdin);#endif//ONL......
  • 同步Linux服务器时间
    0012***/usr/sbin/ntpdate172.17.14.50 https://www.jianshu.com/p/542439e7feb4https://www.cnblogs.com/pipci/p/12844550.html 二、不同机器之间的时间同步为了避免主机时间因为长期运行下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的。Linux系统......