打算用python写一个ls命令脚本,以练习python基础。这个文档就是记录该小项目的进展和学习心得。

ls是Linux下最常用的命令之一,可以列出目录内文件即子目录名,可以配合众多参数使用。为此专门查看了Linux下man手册(我用ceontos7做的测试,其他Linux应该一样。但Windows环境下会有差异,比如Windows隐藏文件的方式与Linux不一样)。

ls功能

--20191107

要用python实现ls命令,那先要了解ls功能,尤其是各种参数。我打算先实现基本功能,然后在逐步添加参数功能。以下是我导出linux系统中ls命令的man手册。

LS(1) User Commands LS(1)

NAME

ls - list directory contents

SYNOPSIS

ls [OPTION]... [FILE]...

DESCRIPTION

List information about the FILEs (the current directory by default). Sort entries alphabetically if none of -cftuvSUX nor

--sort is specified.

Mandatory arguments to long options are mandatory for short options too.

-a, --all

do not ignore entries starting with .

-A, --almost-all

do not list implied . and ..

--author

with -l, print the author of each file

-b, --escape

print C-style escapes for nongraphic characters

--block-size=SIZE

scale sizes by SIZE before printing them; e.g., '--block-size=M' prints sizes in units of 1,048,576 bytes; see SIZE

format below

-B, --ignore-backups

do not list implied entries ending with ~

-c with -lt: sort by, and show, ctime (time of last modification of file status information); with -l: show ctime and

sort by name; otherwise: sort by ctime, newest first

-C list entries by columns

--color[=WHEN]

colorize the output; WHEN can be 'never', 'auto', or 'always' (the default); more info below

-d, --directory

list directories themselves, not their contents

-D, --dired

generate output designed for Emacs' dired mode

-f do not sort, enable -aU, disable -ls --color

-F, --classify

append indicator (one of */=>@|) to entries

--file-type

likewise, except do not append '*'

--format=WORD

across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C

--full-time

like -l --time-style=full-iso

-g like -l, but do not list owner

--group-directories-first

group directories before files;

can be augmented with a --sort option, but any use of --sort=none (-U) disables grouping

-G, --no-group

in a long listing, don't print group names

-h, --human-readable

with -l, print sizes in human readable format (e.g., 1K 234M 2G)

--si likewise, but use powers of 1000 not 1024

-H, --dereference-command-line

follow symbolic links listed on the command line

--dereference-command-line-symlink-to-dir

follow each command line symbolic link

that points to a directory

--hide=PATTERN

do not list implied entries matching shell PATTERN (overridden by -a or -A)

--indicator-style=WORD

append indicator with style WORD to entry names: none (default), slash (-p), file-type (--file-type), classify (-F)

-i, --inode

print the index number of each file

-I, --ignore=PATTERN

do not list implied entries matching shell PATTERN

-k, --kibibytes

default to 1024-byte blocks for disk usage

-l use a long listing format

-L, --dereference

when showing file information for a symbolic link, show information for the file the link references rather than

for the link itself

-m fill width with a comma separated list of entries

-n, --numeric-uid-gid

like -l, but list numeric user and group IDs

-N, --literal

print raw entry names (don't treat e.g. control characters specially)

-o like -l, but do not list group information

-p, --indicator-style=slash

append / indicator to directories

-q, --hide-control-chars

print ? instead of nongraphic characters

--show-control-chars

show nongraphic characters as-is (the default, unless program is 'ls' and output is a terminal)

-Q, --quote-name

enclose entry names in double quotes

--quoting-style=WORD

use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape

-r, --reverse

reverse order while sorting

-R, --recursive

list subdirectories recursively

-s, --size

print the allocated size of each file, in blocks

-S sort by file size

--sort=WORD

sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)

--time=WORD

with -l, show time as WORD instead of default modification time: atime or access or use (-u) ctime or status (-c);

also use specified time as sort key if --sort=time

--time-style=STYLE

with -l, show times using style STYLE: full-iso, long-iso, iso, locale, or +FORMAT; FORMAT is interpreted like in

'date'; if FORMAT is FORMAT1FORMAT2, then FORMAT1 applies to non-recent files and FORMAT2 to recent files;

if STYLE is prefixed with 'posix-', STYLE takes effect only outside the POSIX locale

-t sort by modification time, newest first

-T, --tabsize=COLS

assume tab stops at each COLS instead of 8

-u with -lt: sort by, and show, access time; with -l: show access time and sort by name; otherwise: sort by access

time

-U do not sort; list entries in directory order

-v natural sort of (version) numbers within text

-w, --width=COLS

assume screen width instead of current value

-x list entries by lines instead of by columns

-X sort alphabetically by entry extension

-1 list one file per line

SELinux options:

--lcontext

Display security context. Enable -l. Lines will probably be too wide for most displays.

-Z, --context

Display security context so it fits on most displays. Displays only mode, user, group, security context and file

name.

--scontext

Display only security context and file name.

--help display this help and exit

--version

output version information and exit

SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units are K, M, G, T, P, E, Z, Y (powers of 1024) or

KB, MB, ... (powers of 1000).

Using color to distinguish file types is disabled both by default and with --color=never. With --color=auto, ls emits

color codes only when standard output is connected to a terminal. The LS_COLORS environment variable can change the set‐

tings. Use the dircolors command to set it.

Exit status:

0 if OK,

1 if minor problems (e.g., cannot access subdirectory),

2 if serious trouble (e.g., cannot access command-line argument).

GNU coreutils online help: Report ls translation bugs to

ject.org/team/>

AUTHOR

Written by Richard M. Stallman and David MacKenzie.

COPYRIGHT

Copyright © 2013 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later

.

This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

The full documentation for ls is maintained as a Texinfo manual. If the info and ls programs are properly installed at

your site, the command

info coreutils 'ls invocation'

should give you access to the complete manual.

GNU coreutils 8.22 November 2016 LS(1)

基本功能

之前看材料知道python自带os库。试着写下如下脚本:

import os

doc=os.listdir()

#print(a)

for i in doc:

print(i)

os库的listdir方法返回的是list类型,所以如果直接print(a)的话打印的和ls结果形式不太一样,所以使用了for循环拆开列表。

python中ls是什么_使用python实现ls命令(1)相关推荐

  1. python中的递归思想_〖Python〗-- 递归、面向对象初识及编程思想

    [递归.面向对象初识及编程思想] 一.递归 1.定义: 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. (1)递归就是在过程或函数里调用自身: (2)在使用递归策 ...

  2. python中case的用法_用 Python 实现简单的 switch/case 语句

    在Python中是没有Switch / Case语句的,很多人认为这种语句不够优雅灵活,在Python中用字典来处理多条件匹配问题字典会更简单高效,对于有一定经验的Python玩家不得不承认,的确如此 ...

  3. python中运算的英文_[lemon]Python中的运算符,LemonPython

    算术运算符 +  -  *  /   //(向下取整)  %(取余)  **(幂运算) 比较运算符 == != > >= < <=   返回的是True或False 赋值运算符 ...

  4. python中对列表排序_在Python中对嵌套列表进行排序和分组

    在Python中对嵌套列表进行排序和分组 我具有以下数据结构(列表列表) [ ['4', '21', '1', '14', '2008-10-24 15:42:58'], ['3', '22', '4 ...

  5. python中基本程序结构_关于Python 程序格式框架的描述,正确的是( )

    [判断题]元组的元素是可读的,可以对元组进行更新.增加.删除操作. [多选题]哪些选项关于循环结构的描述是正确的( ) [单选题]以下可以终结一个循环的是 . [判断题]对于大量列表的连接,exten ...

  6. python中类似对象吗_在Python中,两个对象什么时候相同? - python

    似乎2 is 2和3 is 3在python中始终为true,通常,对整数的任何引用都与对相同整数的任何其他引用相同. None(即None is None)也是如此.我知道用户定义类型或可变类型不会 ...

  7. python中nomodulenamed怎么解决_关于 python ImportError: No module named 的问题

    今天在 centos 下安装 python setup.py install 时报错:ImportError: No module named sysconfig, 当时急着用,就顺手直接源码编译了一 ...

  8. python中函数的调用_慢步python,编程中函数的概念,python中函数的声明和调用

    函数,曾经是一个很高大尚的概念.笔者是在高中数学里认识的函数,先是从y=2x+3 这条代数式开始的.y是因变量,x是自变量,y因为x取值的变化而变化. 再后来式子变成这样:f(x)=2x+3,f(x) ...

  9. python中var是什么_这些Python Number 知识你需要了解!

    原标题:这些Python Number 知识你需要了解! 如果把编程比作建房子,那么数据就是建材.而建材有砖头.水泥.木头.钢材等,不同类型的建材配合工作,才能把房子建好.编程也一样,不同类型的数据类 ...

  10. 简述python中面向对象的概念_简述Python中的面向对象编程的概念

    面向对象编程--Object Oriented Programming,简称OOP,是一种程序设计思想.OOP把对象作为程序的基本单元,一个对象包含了数据和操作数据的函数. 面向过程的程序设计把计算机 ...

最新文章

  1. linux内核网络协议栈--linux协议栈调用流程(七)
  2. Java面向对象(10)--super关键字
  3. vue打包后放在 nginx部署时候的配置文件
  4. python程序怎么修改_详解Python文件修改的两种方式
  5. C++ – 第一次作业
  6. 首次启动优美新手指引tip
  7. StanfordDB class自学笔记 (3) 查询关系型数据库总览
  8. kepware omronFINS
  9. mysql 三种循环语句_MySQL循环语句
  10. power apps canvas团队协作开发总结的几种方式
  11. 四则运算生成程序(基于控制台)
  12. 车载无线自组织网络的介质访问控制协议研究
  13. mysql逗号后update_隔mysql逗号
  14. idea的项目模板的创建
  15. 如何处理接口幂等性问题(重复提交)
  16. LiTCTF by lingfeng - (crypto全解)
  17. 163邮箱申请,163.net邮箱成2021年黑马品牌!
  18. 华为公司员工待遇全面揭秘
  19. Unity触摸屏TouchPhase多点触碰旋转放大缩小
  20. Ubuntu18.04/16.04+ Tensorflow1.8 +anaconda安装总结

热门文章

  1. 农夫山泉(数据范围是亮点)
  2. request_threaded_irq
  3. request_threaded_irq()参数
  4. 数据仓库(9)数仓缓慢变化维度数据的处理
  5. 第十届蓝桥杯大赛软件赛省赛——Java大学A组
  6. Android短信数据库相关
  7. 新浪云计算平台应用开发
  8. [转载] 那些极度自律的孩子, 都拥有了开挂的人生
  9. Eclipse平台'Launching test Default' has encountered a problem. Program file not specified解决
  10. 苹果退款48小时审核结果_金苹果花园车辆审核结果20191102