python os模块在系统管理中的应用
作者:sea_kingdom 发布时间:2022-12-17 04:37:23
本文实例为大家分享了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')
来源:https://blog.csdn.net/sea_kingdom/article/details/106106249
猜你喜欢
- 级联样式表在13年前被引入,而且被广泛使用的CSS 2.1 标准在11年前被创建,显然我们现在已经与当年相差千里了。相当了不起的是期间网站开
- 在经过前面几个部分的操作之后,我们的网页已经图文并茂,具有相当的效果了,但是这对于网页来说还不够,为了网站中的众多网页能够成为一个有机的整体
- < SCRIPT LANGUAGE="VBScript"> < 
- 代码如下:---在仓储管理中经常会碰到的一个问题 一、关于LIFO与FIFO的简单说明 ---FIFO: First in, First o
- 一、什么是Oracle字符集 Oracle字符集是一个字节数据的解释的符号集合,有大小之分,有相互的包容关系。ORACLE 支持国家语言的体
- /* JS代码部分 */ 3 const date = new Date()const years = []const months = [
- 如下所示:#! usr/bin/python#coding=utf-8 import numpy as npimport matplotli
- 问题1:使用.net2005自带的SQL-Express连接不上。解决方法:1.网络防火墙阻止数据库连接;2.默认SQL-Express没有
- 对数据库的管理常规就是进行预防性的维护,以及修复那些出现问题的内容。进行检查和修复通常具有四个主要的任务:1. 对表进行优化2. 对表进行分
- 1. 随机搜索策略在本节中,我们将学习一种比随机选择动作更复杂的策略来解决 CartPole 问题—
- 还是一个关于checkbox的一个普通的效果,就是根据你勾选的checkbox,列出你选择了哪些值演示代码:<!DOCTYPE htm
- messageboxtkinter.messagebox中封装了多种消息框,其输入参数统一为title, message以及其他参数。其中t
- CREATE TABLE `category` ( `Id` binary(1
- 经常由于各种压缩格式的不一样用到文件的解压缩时就需要下载不同的解压缩工具去处理不同的文件,以至于桌面上的压缩工具就有三四种,于是使用pyth
- /* * Date Format 1.2.3 * (c) 2007-2009 Steven Levithan * MIT license *
- Set objTextStream=FileSystemObject.CreateTextFile(Filename,[Overwrite]
- 在封装自己的FileSystemObject库的时候,测试的时候发现在文件夹或文件很多的时候,效率很低,显示一个文件夹需要2秒甚至更多,这让
- 引子:今天在蓝点看了Yang的博客《CSS样式表中继承关系的空格与不空格》,思考了一下,本来想写《CSS样式的复合定义与复合调用及简单的模块
- PHP中重定向网页跳转页面的方法(共三种)第一种:利用header()函数进行重定向,这也是我用的较多的。(注意!locationhe和“:
- 字体设计是人类商业活动的需求,它随着时代和科学技术的进步而不断地变化着。被广泛应用于网络生活的各个方面。现代字体设计在计算机技术的应用中已经