python os

os 模块提供了很多允许你的程序与操作系统直接交互的功能

1. 添加os模块

import os

1.1 查看当前工作目录

>>> os.getcwd()    #即当前Python脚本工作的目录路径
'/data/ceshi'

1.2 返回指定目录下的所有文件和目录名

>>> os.listdir()
['3.txt', '4.txt', '9.txt', '5.txt', '7.txt', '8.txt', 'ceshi.txt', '6.txt', '1.txt']

1.3 函数删除一个文件

>>> os.remove("1.txt")
>>> os.listdir()
['3.txt', '4.txt', '9.txt', '5.txt', '7.txt', '8.txt', 'ceshi.txt', '6.txt']

1.4 删除多个目录

>>> os.listdir()
['3.txt', '4.txt', '9.txt', '5.txt', '4', '7.txt', '8.txt', '2', 'ceshi.txt', '6.txt', '3']
>>> os.removedirs(r"/data/ceshi/2/python")
>>> os.removedirs(r"/data/ceshi/3/python")
>>> os.removedirs(r"/data/ceshi/4/python")
>>> os.listdir()
['3.txt', '4.txt', '9.txt', '5.txt', '7.txt', '8.txt', 'ceshi.txt', '6.txt']

1.5 检验给出的路径是否是一个文件

>>> os.path.isfile("3.txt")
True

1.6 检验给出的路径是否是一个目录

>>> os.listdir()
['3.txt', '4.txt', '9.txt', '5.txt', '7.txt', '8.txt', 'os', 'ceshi.txt', '6.txt']
>>> os.path.isdir("3.txt")
False
>>> os.path.isdir("os")
True

1.7 判断是否为绝对路径

>>> os.path.isabs("os")
False
>>> os.path.isabs("/data/ceshi/os")
True

1.8 检验给出的路径是否为真是存在的

>>> os.path.exists("/data/ceshi/")
True
>>> os.path.exists("/data/cesh/")
False

1.9 返回一个路径的目录名和文件名

>>> os.path.split("/data/ceshi/4.txt")
('/data/ceshi', '4.txt')

1.10  分离拓展名

>>> os.path.splitext("3.txt")
('3', '.txt')

1.11获取路径名

>>> os.path.dirname("/data/ceshi/3.txt")
'/data/ceshi'

1.12 获取文件名

>>> os.path.basename("/data/ceshi/3.txt")
'3.txt'

1.13 获取绝对路径

>>> os.path.abspath("/data/ceshi/3.txt")
'/data/ceshi/3.txt'
>>> os.path.abspath("3.txt")
'/data/ceshi/3.txt'

1.14 运行shell命令

>>> os.system("ls -l")
total 8
-rw-r--r-- 1 root root    0 Jan  6 17:36 3.txt
-rw-r--r-- 1 root root    0 Jan  6 17:36 4.txt
-rw-r--r-- 1 root root    0 Jan  6 17:36 5.txt
-rw-r--r-- 1 root root    0 Jan  6 17:36 6.txt
-rw-r--r-- 1 root root    0 Jan  6 17:36 7.txt
-rw-r--r-- 1 root root    0 Jan  6 17:36 8.txt
-rw-r--r-- 1 root root    0 Jan  6 17:36 9.txt
-rw-r--r-- 1 root root    6 Jan  6 17:10 ceshi.txt
drwxr-xr-x 2 root root 4096 Jan  6 17:48 os
0
>>> os.system("rm -rf 3.txt 4.txt 5.txt")
0
>>> os.system("ls -l")
total 8
-rw-r--r-- 1 root root    0 Jan  6 17:36 6.txt
-rw-r--r-- 1 root root    0 Jan  6 17:36 7.txt
-rw-r--r-- 1 root root    0 Jan  6 17:36 8.txt
-rw-r--r-- 1 root root    0 Jan  6 17:36 9.txt
-rw-r--r-- 1 root root    6 Jan  6 17:10 ceshi.txt
drwxr-xr-x 2 root root 4096 Jan  6 17:48 os
0

1.15 读取操作系统HOME的环境变量

>>> os.getenv("HOME")
'/root'

1.16 返回操作系统的所有环境变量

>>> os.environ
environ({'XDG_SESSION_ID': '12869', 'HOSTNAME': 'kubernetes', 'TERM': 'xterm', 'SHELL': '/bin/bash', 'HISTSIZE': '2000', 'SSH_CLIENT': '115.171.120.153 30779 22', 'OLDPWD': '/data', 'SSH_TTY': '/dev/pts/0', 'HISTFILESIZE': '2000', 'USER': 'root', 'LS_COLORS': 'rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:mi=01;05;37;41:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arc=01;31:*.arj=01;31:*.taz=01;31:*.lha=01;31:*.lz4=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.tzo=01;31:*.t7z=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lrz=01;31:*.lz=01;31:*.lzo=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.alz=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.cab=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=01;36:*.au=01;36:*.flac=01;36:*.mid=01;36:*.midi=01;36:*.mka=01;36:*.mp3=01;36:*.mpc=01;36:*.ogg=01;36:*.ra=01;36:*.wav=01;36:*.axa=01;36:*.oga=01;36:*.spx=01;36:*.xspf=01;36:', 'MAIL': '/var/spool/mail/root', 'PATH': '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin', 'PWD': '/data/ceshi', 'LANG': 'en_US.UTF-8', 'HISTCONTROL': 'ignoredups', 'SHLVL': '1', 'HOME': '/root', 'LOGNAME': 'root', 'SSH_CONNECTION': '115.171.120.153 30779 172.26.132.75 22', 'LESSOPEN': '||/usr/bin/lesspipe.sh %s', 'XDG_RUNTIME_DIR': '/run/user/0', '_': '/usr/bin/python3'})

1.17 设置系统环境变量,仅程序运行时有效

>>> os.environ.setdefault("HOME","/home/chaopeng")
'/root'

1.18 给出当前平台使用的终止符

>>> os.linesep
'\n'

1.19 提示正在使用的平台

>>> os.name
'posix'

1.20 重命名

>>> os.listdir()
['9.txt', '7.txt', '8.txt', 'os', 'ceshi.txt', '6.txt']
>>> os.rename("9.txt", "py.txt")
>>> os.listdir()
['py.txt', '7.txt', '8.txt', 'os', 'ceshi.txt', '6.txt']

1.21 创建多级目录

>>> os.makedirs(r"/data/ceshi/python/linux")

1.22 创建单个目录

>>> os.mkdir("nis")
>>> os.listdir()
['python', 'py.txt', '7.txt', '8.txt', 'os', 'ceshi.txt', '6.txt', 'nis']

1.23 获取文件属性

>>> os.listdir()
['python', 'py.txt', '7.txt', '8.txt', 'os', 'ceshi.txt', '6.txt', 'nis']
>>> os.stat("nis")
os.stat_result(st_mode=16877, st_ino=1317936, st_dev=64769, st_nlink=2, st_uid=0, st_gid=0, st_size=4096, st_atime=1609929491, st_mtime=1609929491, st_ctime=1609929491)
>>> os.stat("py.txt")
os.stat_result(st_mode=33188, st_ino=1317941, st_dev=64769, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1609925786, st_mtime=1609925786, st_ctime=1609929351)

1.24 修改文件权限和时间戳

>>> os.chmod("py.txt",775)

1.25 获取文件大小

>>> os.path.getsize("ceshi.txt")
6

1.26 结合目录名与文件名

>>> os.path.join("/data/ceshi","py.txt")
'/data/ceshi/py.txt'

1.27 改变工作目录到direname

>>> os.chdir("/data/ceshi/os")
>>> os.listdir()
[]

1.28  获取当前终端的大小

>>> os.get_terminal_size()
os.terminal_size(columns=263, lines=57)

1.29 杀死进程

os.kill(10884,signal.SIGKILL)

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值