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按照多个字符对字符串进行分割的方法
Mar 17 Python
python中numpy基础学习及进行数组和矢量计算
Feb 12 Python
TF-IDF与余弦相似性的应用(二) 找出相似文章
Dec 21 Python
python利用sklearn包编写决策树源代码
Dec 21 Python
快速了解Python相对导入
Jan 12 Python
python基础梳理(一)(推荐)
Apr 06 Python
Python3.5字符串常用操作实例详解
May 01 Python
python如何实现异步调用函数执行
Jul 08 Python
python使用正则来处理各种匹配问题
Dec 22 Python
python实现FTP循环上传文件
Mar 20 Python
Python使用monkey.patch_all()解决协程阻塞问题
Apr 15 Python
基于PyQT实现区分左键双击和单击
May 19 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文件
2007/01/04 PHP
PHP has encountered an Access Violation
2007/01/15 PHP
thinkphp区间查询、统计查询与SQL直接查询实例分析
2014/11/24 PHP
PHP中COOKIES使用示例
2015/07/26 PHP
通过代码实例解析PHP session工作原理
2020/12/11 PHP
javascript中的有名函数和无名函数
2007/10/17 Javascript
jQuery 位置插件
2008/12/25 Javascript
被jQuery折腾得半死,揭秘为何jQuery为何在IE/Firefox下均无法使用
2010/01/22 Javascript
js获取浏览器的可视区域尺寸的实现代码
2011/11/30 Javascript
javascript时间自动刷新实现原理与步骤
2013/01/06 Javascript
微信小程序(三):网络请求
2017/01/13 Javascript
Vue.js做select下拉列表的实例(ul-li标签仿select标签)
2018/03/02 Javascript
Vue与Node.js通过socket.io通信的示例代码
2018/07/25 Javascript
使用vue-cli3 创建vue项目并配置VS Code 自动代码格式化 vue语法高亮问题
2019/05/14 Javascript
微信小程序实现下拉刷新动画
2019/06/21 Javascript
python中from module import * 的一个坑
2014/07/20 Python
给你选择Python语言实现机器学习算法的三大理由
2017/11/15 Python
对命令行模式与python交互模式介绍
2018/05/12 Python
python中itertools模块zip_longest函数详解
2018/06/12 Python
对python 生成拼接xml报文的示例详解
2018/12/28 Python
Djang的model创建的字段和参数详解
2019/07/27 Python
Python线程协作threading.Condition实现过程解析
2020/03/12 Python
Python Opencv中用compareHist函数进行直方图比较对比图片
2020/04/07 Python
Python小白不正确的使用类变量实例
2020/05/29 Python
python如何写try语句
2020/07/14 Python
python 爬取英雄联盟皮肤并下载的示例
2020/12/04 Python
彪马加拿大官网:PUMA加拿大
2018/10/04 全球购物
财务会计专业毕业生自荐信
2013/10/19 职场文书
大学生如何写自荐信
2014/01/08 职场文书
营销总经理岗位职责
2014/02/02 职场文书
机械操作工岗位职责
2014/08/08 职场文书
校园游戏活动新闻稿
2014/10/15 职场文书
2015年幼儿园个人工作总结
2015/04/25 职场文书
单位工作证明范本
2015/06/15 职场文书
高中团支书竞选稿
2015/11/21 职场文书
Canvas跟随鼠标炫彩小球的实现
2021/04/11 Javascript