首页 > 技术文章 > 人生苦短我用python 初窥自动化运维之os模块

FrancisDrakeK 2018-09-10 19:35 原文

人生苦短我用python

 初窥自动化运维之os模块

 

 

#python os模块

 

os.system

执行系统命令,但是不返回结果

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
os.system("ls")

image
#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
os.system("echo $(ifconfig)>text.txt")

 

image

os.popen

执行系统命令,但是以文件的形式返回结果

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
my=os.popen("echo hello")
#print(my.read())

 

image
#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
my=os.popen("echo hello")
print(my.read())

 

image

os.listdir 


返回指定目录下的文件和目录。想要列出目录内容则需要print(os.listdir(""))

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
os.listdir("")

 

os.sep

 

 

取代操作系统特定的路径分割符,用于跨平台 因为平台之间存在分隔符不同的问题所以使用sep

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
print(os.sep)

 

windos下结果 

linux下结果

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
mypath=os.sep+"usr"+os.sep+"local"+os.sep+"bin"+os.sep
print(mypath)

 

windos下结果

linux下结果

 

os.getcwd

获取当前的工作目录

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
print(os.getcwd())

 

image

 

os.remove

删除文件

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
os.remove("whocanremoveme.bug")

 

我们在家目录下创建whocanremoveme.bug
运行
#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
os.remove(os.sep+"root"+os.sep+"whocanremoveme.bug")

 

 


os.chdir

 

修改执行路径

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
print(os.getcwd())
os.chdir("/root")
print(os.getcwd())

 


os.mkdir

创建目录

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
os.mkdir(os.sep+"root"+os.sep+"mynewdir")

 



 

os.path.join

 

路径拼接(自适应平台的和os.sep)

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
# os.mkdir(os.sep+"root"+os.sep+"mynewdir")
print(os.path.join("hello","cs"))

 

windows下

linux下


os.path.isfile

 

判断是否是文件


os.path.isdir

 

判断是否是文件夹

 

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    : 
# @File    : E.py
# @Software: PyCharm
import os
src=os.path.join(os.sep+"root","anaconda-ks.cfg")
print(src)
src2=os.path.join(os.sep+"root","mypy")
print(src2)
print(os.path.isdir(src))
print(os.path.isfile(src))
print(os.path.isdir(src2))
print(os.path.isfile(src2))

 


os.path.split

路径切分

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    :
# @File    : E.py
# @Software: PyCharm
import os
src=os.path.join(os.sep+"root","anaconda-ks.cfg")
src2=os.sep+"root"+os.sep+"mypy"+os.sep+"E.py"
print(src)
print(os.path.split(src))
print(src2)
print(os.path.split(src2))

 


os.path.dirname

返回指定的目录

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    :
# @File    : E.py
# @Software: PyCharm
import os
print(os.path.dirname("/"))

 

经常和__file__一起使用用来显示当前目录到python脚本之间位置的相对路径
#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    :
# @File    : E.py
# @Software: PyCharm
import os
print(os.path.dirname(__file__))

 


os.rename

修改名称

#!/usr/bin/python36
# -*- coding: utf-8 -*-
# @Time    : 2018-09-07 16:20
# @Author  : FrancisDrakeK
# @Site    :
# @File    : E.py
# @Software: PyCharm
import os
os.rename("text.txt","hello.txt")

 







推荐阅读