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的Flask框架的简介和安装方法
Nov 13 Python
python文件操作相关知识点总结整理
Feb 22 Python
深入理解python对json的操作总结
Jan 05 Python
CentOS7.3编译安装Python3.6.2的方法
Jan 22 Python
python实现将文件夹下面的不是以py文件结尾的文件都过滤掉的方法
Oct 21 Python
很酷的python表白工具 你喜欢我吗
Apr 11 Python
Python 学习教程之networkx
Apr 15 Python
Python实现Mysql数据统计及numpy统计函数
Jul 15 Python
tensorflow的计算图总结
Jan 12 Python
Python爬虫实例——scrapy框架爬取拉勾网招聘信息
Jul 14 Python
python实现批处理文件
Jul 28 Python
Django实现WebSocket在线聊天室功能(channels库)
Sep 25 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
新版mysql+apache+php Linux安装指南
2006/10/09 PHP
sphinx增量索引的一个问题
2011/06/14 PHP
yii框架表单模型使用及以数组形式提交表单数据示例
2014/04/30 PHP
30个php操作redis常用方法代码例子
2014/07/05 PHP
PHP实现的同步推荐操作API接口案例分析
2016/11/30 PHP
PHP中让json_encode不自动转义斜杠“/”的方法
2017/02/28 PHP
jquery $.ajax相关用法分享
2012/03/16 Javascript
jQuery$命名冲突怎么办如何解决
2014/01/16 Javascript
Angularjs 创建可复用组件实例代码
2016/10/09 Javascript
干货!教大家如何选择Vue和React
2017/03/13 Javascript
jQuery异步提交表单实例
2017/05/30 jQuery
微信小程序之电影影评小程序制作代码
2017/08/03 Javascript
浅谈JS 数字和字符串之间相互转化的纠纷
2017/10/20 Javascript
vue 检测用户上传图片宽高的方法
2020/02/06 Javascript
vue实现单一筛选、删除筛选条件
2020/10/26 Javascript
[05:08]2014DOTA2国际邀请赛 Hao专访复仇的胜利很爽
2014/07/15 DOTA
线程和进程的区别及Python代码实例
2015/02/04 Python
python实现JAVA源代码从ANSI到UTF-8的批量转换方法
2015/08/10 Python
Python简单计算给定某一年的某一天是星期几示例
2018/06/27 Python
python复制列表时[:]和[::]之间有什么区别
2018/10/16 Python
python实现桌面气泡提示功能
2019/07/29 Python
Python生成验证码、计算具体日期是一年中的第几天实例代码详解
2019/10/16 Python
python tornado使用流生成图片的例子
2019/11/18 Python
Python中filter与lambda的结合使用详解
2019/12/24 Python
Python selenium键盘鼠标事件实现过程详解
2020/07/28 Python
python将下载到本地m3u8视频合成MP4的代码详解
2020/11/24 Python
html5如何及时更新缓存文件(js、css或图片)
2013/06/24 HTML / CSS
波兰汽车配件网上商店:iParts.pl
2020/09/08 全球购物
化工机械应届生求职信
2013/11/04 职场文书
法学专业毕业生自荐信范文
2013/12/18 职场文书
基层党支部公开承诺书
2014/05/29 职场文书
开展批评与自我批评发言稿
2014/10/16 职场文书
2014年助理政工师工作总结
2014/12/19 职场文书
离婚财产分割协议书
2015/08/11 职场文书
高中物理教学反思
2016/02/19 职场文书
十大动画制作软件,Adobe产品上榜两款,第一是行业标准软件
2022/03/18 杂记