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使用random和tertools模块解一些经典概率问题
Jan 28 Python
python利用装饰器进行运算的实例分析
Aug 04 Python
使用Python的urllib和urllib2模块制作爬虫的实例教程
Jan 20 Python
Python selenium 三种等待方式解读
Sep 15 Python
python批量设置多个Excel文件页眉页脚的脚本
Mar 14 Python
python程序封装为win32服务的方法
Mar 07 Python
用django-allauth实现第三方登录的示例代码
Jun 24 Python
python爬取盘搜的有效链接实现代码
Jul 20 Python
如何利用Python开发一个简单的猜数字游戏
Sep 22 Python
python Event事件、进程池与线程池、协程解析
Oct 25 Python
pytorch实现MNIST手写体识别
Feb 14 Python
python定义具名元组实例操作
Feb 28 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 chmod 函数与批量修改文件目录权限
2010/05/10 PHP
php jquery 多文件上传简单实例
2013/12/23 PHP
php删除txt文件指定行及按行读取txt文档数据的方法
2017/01/30 PHP
Thinkphp5 微信公众号token验证不成功的原因及解决方法
2017/11/12 PHP
firefox火狐浏览器与与ie兼容的2个问题总结
2010/07/20 Javascript
克隆javascript对象的三个方法小结
2011/01/12 Javascript
jQuery 源码分析笔记(6) jQuery.data
2011/06/08 Javascript
7款风格新颖的jQuery/CSS3菜单导航分享
2013/04/23 Javascript
js判断FCKeditor内容是否为空的两种形式
2013/05/14 Javascript
js导入导出excel(实例代码)
2013/11/25 Javascript
使用CSS样式position:fixed水平滚动的方法
2014/02/19 Javascript
jQuery Validate 验证,校验规则写在控件中的具体实例
2014/02/27 Javascript
JavaScript的作用域和块级作用域概念理解
2014/09/21 Javascript
Angularjs 基础入门
2014/12/26 Javascript
javascript比较两个日期相差天数的方法
2015/07/24 Javascript
基于javascript简单实现对身份证校验
2021/01/25 Javascript
用nodejs搭建websocket服务器
2017/01/23 NodeJs
详解Jquery EasyUI tree 的异步加载(遍历指定文件夹,根据文件夹内的文件生成tree)
2017/02/11 Javascript
Angular.js自定义指令学习笔记实例
2017/02/24 Javascript
Vue通过URL传参如何控制全局console.log的开关详解
2017/12/07 Javascript
jQuery实现页码跳转式动态数据分页
2017/12/31 jQuery
nodejs中实现用户注册路由功能
2019/05/20 NodeJs
sortable+element 实现表格行拖拽的方法示例
2019/06/07 Javascript
nginx部署多个vue项目的方法示例
2020/09/06 Javascript
python数据结构之二叉树的统计与转换实例
2014/04/29 Python
基于python的七种经典排序算法(推荐)
2016/12/08 Python
python装饰器实例大详解
2017/10/25 Python
Python 解决中文写入Excel时抛异常的问题
2018/05/03 Python
Python设计模式之装饰模式实例详解
2019/01/21 Python
解决python有时候import不了当前的包问题
2019/08/28 Python
Python中bisect的使用方法
2019/12/31 Python
基于html5实现的图片墙效果
2014/10/16 HTML / CSS
应聘护士自荐信
2013/10/21 职场文书
毕业生简单求职信
2013/11/19 职场文书
反洗钱宣传活动总结
2014/08/26 职场文书
财务会计实训报告
2014/11/05 职场文书