python os模块在系统管理中的应用


Posted in Python onJune 22, 2020

本文实例为大家分享了python os模块在系统管理中的应用代码,供大家参考,具体内容如下

#临时文件

import tempfile 
tempfile.gettempdir()
#'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp'

tempfile.mkstemp()
#(4, 'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmp9zc5ipzr')

tempfile.mkdtemp()
#'C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\tmp94wxuh44'

#操作系统命令

import os
os.chdir(r'd:')
#切换到目录(r为转义字符)

os.listdir(r'd:')
#显示目录下的所有文件

os.makedirs(r'd:\1\1')
#创建路径的所有文件

os.mkdir(r'd:\1')
#创建文件

#查找

import glob
glob.glob('d:*.txt')
#目录下的txt文件
glob.glob('d:*n.txt')
#目录下的以n.txt结尾的文件

#遍历目录

import re,os,os.path 

def run(top):
 for(dirname,subdirs,files) in os.walk(top):
 print("["+dirname+"]")
 for fname in files:
  print(os.path.join(dirname,fname))
if __name__=='__main__':
 run(r'd:\1')

调用以下函数时要注意以下两点

(1)调用任何函数之前,要先调用start()函数。要有d:\ptest、和ptest下有三个目录:document、files、temp,才能进行其他操作
(2)调用(1)-(8)函数,只需要test8()

例如:解决第八个问题

start()
test8()

****d:\ptest、ptest下有三个目录:document、files、temp。

import os,glob,shutil

def start():
 if os.path.exists(r'd:\ptest'):
 pass
 else:
 os.makedirs(r'd:\ptest\document')
 os.makedirs(r'd:\ptest\files')
 os.makedirs(r'd:\ptest\temp')

(1)将c:\windows目录下的所有ini文件复制到document中。

def test1():
 file_lists=glob.glob('c:\windows\*.ini')
 for file in file_lists:
 shutil.copy(file,r'd:\ptest\document')

(2)将c:\windows目录下以'n'开头的所有文件复制到files中。

def test2():
 file_lists=glob.glob('c:\windows\*')
 #temp=[]#以'n'开头的所有文件
 for file in file_lists:
 files=file.replace('c:\windows\\','')
 if files.startswith('n'):
  shutil.copy(file,r'd:\ptest\files')
  #temp.append(file)

(3)判断files文件夹中是否有notepad.exe文件,如果有,将其复制到temp中,并改名为mypad.exe。

def test3():
 if os.path.exists(r'd:\ptest\files\notepad.exe'):
 shutil.copy(r'd:\ptest\files\notepad.exe',r'd:\ptest\temp\mypad.exe')
 else:
 print("没有notepad.exe文件")

(4)判断document文件夹中是否有win.ini文件,如果有将其移动到temp中。

def test4():
 if os.path.exists(r'd:\ptest\document\win.ini'):
 shutil.move(r'd:\ptest\document\win.ini',r'd:\ptest\temp')
 else:
 print("没有win.ini文件")

(5)判断document文件夹中是否有system.ini文件,如果有将其以system.inf的名称复制到temp中,然后删除原文件。

def test5():
 if os.path.exists(r'd:\ptest\document\system.ini'):
 #复制删除
 shutil.copy(r'd:\ptest\document\system.ini',r'd:\ptest\temp\system.inf')
 os.remove(r'd:\ptest\document\system.ini')
 
 #移动
 #shutil.move(r'd:\ptest\document\system.ini',r'd:\ptest\temp')
 else:
 print("没有system.ini文件")

(6)在document下新建mydir文件夹,并将temp中的所有文件复制到mydir下。

def test6():
 if os.path.exists(r'd:\ptest\document\mydir'):
 pass
 else:
 os.mkdir(r'd:\ptest\document\mydir')
 
 '''#遍历找出文件
 for (dirpath,dirnames,filenames)in os.walk(r'd:\ptest\document'):
 for file in filenames:
  print(os.path.join(dirpath,file))
 '''
 file_lists=glob.glob('d:\ptest\document\*')
 for file in file_lists:
 if os.path.isfile(file):
  if os.path.exists(file):
  print("文件已存在")
  else:
  shutil.copy(file,r'd:\ptest\document\mydir')

(7)将files目录及其内部所有文件以myfiles目录名整体复制到mydir下,然后删除原来的整个files目录及其内部的所有文件。

def test7():
 #移动
 shutil.move(r'd:\ptest\files',r'd:\ptest\document\mydir\myfiles')
 
 '''#复制,删除
 file_lists=glob.glob(r'd:\ptest\files\*')
 print(file_lists)
 if os.path.exists(r'd:\ptest\document\mydir\myfiles'):
 pass
 else:
 os.mkdir(r'd:\ptest\document\mydir\myfiles')
 for file in file_lists:
 shutil.copy(file,r'd:\ptest\document\mydir\myfiles')
 os.remove(file)
 os.rmdir(r'd:\ptest\files')
 '''

(8)找到此时notepad.exe文件的所在路径,输出其创建时间、最近访问时间和最近修改时间,在输出给文件的大小。

def find(top,name):  #find与next_find形成一个轮回,只有发现文件,或文件夹为空时跳出
 for (dirpath,dirnames,filenames) in os.walk(top):
 for file in filenames:
  if file==name:
  return os.path.join(dirpath,file)
 for dirs in dirnames:
  if dirs==name:
  return os.path.join(dirpath,dirs)
 #说明上述文件和目录中无查找内容,将目录列表发给next_find函数
 next_find(dirnames,top,name)
 
def next_find(dirnames,top,name):
 for temp in dirnames:
  #目录为空时跳出
  if not temp :
  break
  #更改遍历目录
  top=os.path.join(top,temp)
  #print(top)
  
  find(top,name)
import time
def test8():
 #将时间转换为时间参数
 geta=time.gmtime(os.path.getatime(find(r'd:\ptest','win.ini')))
 getm=time.gmtime(os.path.getmtime(find(r'd:\ptest','win.ini')))
 getc=time.gmtime(os.path.getctime(find(r'd:\ptest','win.ini')))
 #将时间参数转换为标准时间
 print("最近访问时间",time.strftime('%c',geta))
 print("最近修改时间",time.strftime('%c',getm))
 print("创建时间",time.strftime('%c',getc))
 print('大小%.3f'%(os.stat(find(r'd:\ptest','win.ini')).st_size/1024),'kB')

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python发送arp欺骗攻击代码分析
Jan 16 Python
Python 字符串操作方法大全
Mar 11 Python
Python中模块string.py详解
Mar 12 Python
Python3结合Dlib实现人脸识别和剪切
Jan 24 Python
python取代netcat过程分析
Feb 10 Python
使用Python搭建虚拟环境的配置方法
Feb 28 Python
Django添加favicon.ico图标的示例代码
Aug 07 Python
django项目搭建与Session使用详解
Oct 10 Python
Python基于plotly模块实现的画图操作示例
Jan 23 Python
Python3.5面向对象程序设计之类的继承和多态详解
Apr 24 Python
Python实现检测文件的MD5值来查找重复文件案例
Mar 12 Python
Python数据可视化图实现过程详解
Jun 12 Python
解决tensorflow读取本地MNITS_data失败的原因
Jun 22 #Python
python实现猜数游戏(保存游戏记录)
Jun 22 #Python
基于Tensorflow读取MNIST数据集时网络超时的解决方式
Jun 22 #Python
在Mac中配置Python虚拟环境过程解析
Jun 22 #Python
tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this T
Jun 22 #Python
TensorFlow的环境配置与安装教程详解(win10+GeForce GTX1060+CUDA 9.0+cuDNN7.3+tensorflow-gpu 1.12.0+python3.5.5)
Jun 22 #Python
keras的load_model实现加载含有参数的自定义模型
Jun 22 #Python
You might like
PHP截取汉字乱码问题解决方法mb_substr函数的应用
2008/03/30 PHP
那些年我们错过的魔术方法(Magic Methods)
2014/01/14 PHP
yii2利用自带UploadedFile实现上传图片的示例
2017/02/16 PHP
PHP结合Ffmpeg快速搭建流媒体服务的实践记录
2018/10/31 PHP
filemanage功能中用到的lib.js
2007/04/08 Javascript
doctype后如何获得body.clientHeight的方法
2007/07/11 Javascript
JS清除IE浏览器缓存的方法
2013/07/26 Javascript
javascript圆盘抽奖程序实现原理和完整代码例子
2014/06/03 Javascript
使用jQuery.wechat构建微信WEB应用
2014/10/09 Javascript
jQuery中first()方法用法实例
2015/01/06 Javascript
NodeJS中利用Promise来封装异步函数
2015/02/25 NodeJs
javascript中数组和字符串的方法对比
2016/07/20 Javascript
Vuejs第十三篇之组件——杂项
2016/09/09 Javascript
浅谈JS之iframe中的窗口
2016/09/13 Javascript
javascript跨域请求包装函数与用法示例
2016/11/03 Javascript
jQuery设置和获取select、checkbox、radio的选中值方法
2017/01/01 Javascript
解析Vue2.0双向绑定实现原理
2017/02/23 Javascript
基于jQuery Easyui实现登陆框界面
2017/07/10 jQuery
angular中两种表单的区别(响应式和模板驱动表单)
2018/12/06 Javascript
微信小程序获取位置展示地图并标注信息的实例代码
2019/09/01 Javascript
详解Vue 单文件组件的三种写法
2020/02/19 Javascript
Python采用Django制作简易的知乎日报API
2016/08/03 Python
对Python 网络设备巡检脚本的实例讲解
2018/04/22 Python
Python使用selenium实现网页用户名 密码 验证码自动登录功能
2018/05/16 Python
python numpy 显示图像阵列的实例
2018/07/02 Python
Python简单过滤字母和数字的方法小结
2019/01/09 Python
Python3中urlencode和urldecode的用法详解
2019/07/23 Python
django 连接数据库 sqlite的例子
2019/08/14 Python
PyQt5实现仿QQ贴边隐藏功能的实例代码
2020/05/24 Python
为什么说python适合写爬虫
2020/06/11 Python
最新PyCharm 2020.2.3永久激活码(亲测有效)
2020/11/26 Python
整理的15个非常有用的 HTML5 开发教程和速查手册
2011/10/18 HTML / CSS
写一个在SQL Server创建表的SQL语句
2012/03/10 面试题
行政助理的职责
2013/11/14 职场文书
银行员工辞职信范文
2014/01/20 职场文书
庆祝教师节活动方案
2014/01/31 职场文书