python3 常用模块(时间、os路径及文件操作、字符串比较,文件内容比较等模块)

python里面有很多模块,以实现各种功能,学习python,我个人认为最重要的是熟练掌握模块的功能,灵活应用,首先就要掌握基础模块。

本文的主要模块包括:时间的模块(time,datetime和calendar)os模块(路径操作模块os.path,读写文件模块open(),多个文件读写处理模块fileinput,创建当前文件模块tempfile等),文件本身的信息访问os.stat(path)和字符串的读取textwrap模块、字符串比较模块difflib和文件比较模块filecmp。


1. 时间的模块(time,datetime和calendar)

    这三个模块就是python对时间的处理,包括时间的访问,时差计算,查看日历,等等。常用的操作如下:

<span style="font-size:12px;">import time
import datetime
import calendar

now  = datetime.datetime.now()
print(now.strftime('%Y-%m-%d %H:%M:%S')) #输出日期、时间的格式(字符串变时间格式)

time_strA = '2014-07-29 01:15:00'
time_strB ='2014-08-29 01:15:00'
#  .strptime()函数 为时间格式转变为字符串个格式
day = datetime.datetime.strptime(time_strA, '%Y-%m-%d %H:%M:%S')
day2 = datetime.datetime.strptime(time_strB, '%Y-%m-%d %H:%M:%S')
sub_day = day2 - day
print('{0}和{1}相差{2}天'.format(time_strA, time_strB, str(sub_day.days)))
#时间延迟 用datetime.timedelta()函数 今后一个星期零三天
N=3
M = 1
N_date = datetime.timedelta(weeks = 1,days = N)
day = now + N_date
print(day.strftime('%Y-%m-%d %H:%M:%S'))</span>
<pre name="code" class="python"><span style="font-size:12px;">
</span>
<span style="font-size:12px;">#日历的常见函数
calendar.month(year, month)
calendar.calendar(year)
calendar.isleap(year)#判断是否是闰年
calendar.monthrange(year, month)#返回指定年的月的日期
calendar.monthcalendar(year, month)#指定年月的周</span>





2. os模块

     首先,这个模块为操作系统的函数模块,涵盖其他的许多模块,如:路径操作模块os.path,读写文件模块open(),多个文件读写处理模块fileinput,创建当前文件模块tempfile等。


A . os和os.path模块常见运用

<span style="font-size:12px;">import os
pa = os.getcwd()#获取当前工作目录
print(os.listdir(pa))#路径pa中的文件及文件夹展开
path = r'C:\test.html'
print(os.path.abspath(path)) #绝对路径
print(os.path.dirname(path)) #路径名的文件夹名称
print(os.path.getatime(path)) #最后一次访问时间,还有其他的函数,如最后一次修改时间等
print(os.path.isfile(path))#判断是否为文件,还有路径分割等os.path.split(path)、os.<tt class="descclassname">path.</tt><tt class="descname">splitext</tt><big>(</big><em>path</em><big>)</big>及合并os.path.jion()
print(os.path.exists(path))#判断路径是否存在
print(os.path.isdir(path)) #是否是文件夹
print(os.path.getsize(path))#文件大小</span>


B. 文件操作open()、fileinput和tempfile模块

   最常用的就是单个文件的读写操作,包括二进制的读写,open()如下:

<span style="font-size:12px;">import os
path = r'C:\test.html'

fp = open(path,'w+')#写操作打开
#fp = open(path,'w+b') 二进制写操作
content = 'adf'
fp.write(content)
fp.flush()
fp.close()
fp = open(path,'r+')#读
print('读取文件:{0}所有内容:{1}'.format(path,fp.readlines())) </span>


fileinput模块对多个文件处理,则路径存文tuple类型,如下:

<span style="font-size:12px;">import os
import fileinput

paths = (r'C:\test.html',r'C:\test.txt')

fp = fileinput.input(paths) #载入
lines = ''
names = []
for line in fp:
    lines +=line  #两个文件所有内容一起读取到lines中
    name = fileinput.filename()#文件名
    names.append(name)
print(lines)
</span>


tempfile在缓存中创建零时文件(夹), 其他程序不能共享该缓存中的零时文件, 因为它并没有引用 文件系统表。而用 TemporaryFile 这个函数创建的临时文件,关闭文件后 会自动删除。

import os
import tempfile

path = r'C:\test.%s.txt'%os.getpid()

fp = open(path,'w+b') #载入
content = b'ccc'  #要变为二进制才能写
fp.write(content)
fp.close()
os.remove(path) #手动删除零时文件
#用TemporaryFile来创建,关闭文件时自动删除
fil = tempfile.TemporaryFile()
fil.write(b'ddd')
fil.seek(0)
print(fil.read())
fil.close() #关闭时,零时文件自动删除


3. 文件本身的信息访问os.stat(path)和字符串的读取 textwrap模块、字符串比较模块difflib和文件比较模块filecmp

os.stat(path)是对文件本身信息的访问,有是个属性,如下:

import os
import stat
st_mode    -- protection
st_ino     -- inode number(索引号)
st_nlink   -- number of hard links(硬链接号)
st_uid     -- user id of owner(用户id)
st_gid     -- group id of owner (组id)
st_size    -- size of file,in bytes (大小)
st_atime   -- time of most recent access expressed in seconds (访问时间)
st_mtime   -- time of most recent content modificatin expressed in seconds (修改时间)
st_ctime
#访问格式为
os.stat(path).st_size 或 os.stat(path)[stat.ST_SIZE]
这两种表示方法是一样的。
字符串读取textwrap,包括wrap(),fill(),dedent()等方法

import textwrap

test_content = '''dakjhdklghkjaghsaghskghskfh'''
print(textwrap.wrap(test_comntent,5))#将字符串test_content按长度5切片为元组
print(textwrap.fill(test_conten,10))#将字符串test_content按每行10个字符拆开并且逐行显示
print(textwrap.dedent(test_conten))#不缩进显示,即换行符并不换行,此行继续显示


文件内容、字符串比较模块difflib,比较后可以用模块HtmlDiff模块来显示在文件.html中(表格):

import difflib
import os
from difflib import HtmlDiff
if os.path.exists('C:\\test.html'):
    with open('C:\\test.html','w+') as fp:
        test = HtmlDiff.make_file(HtmlDiff(), 'hello world!', 'hElLO Wor2d!')
        fp.write(test)
        print('生成文件成功!')
        fp.close()
#或者如下
test = difflib.Differ().compare('hello world', 'HeLLO,wOrlD!')
print('横向展示:')
print(''.join(list(test)))

还有一个比较文件内容的filecmp模块,转载来源: http://scm002.iteye.com/blog/1662812

filecmp模块用于比较文件及文件夹的内容,它是一个轻量级的工具,使用非常简单。python标准库还提供了difflib模块用于比较文件的内容。关于difflib模块,且听下回分解。

    filecmp定义了两个函数,用于方便地比较文件与文件夹:

filecmp.cmp(f1, f2[, shallow]):

    比较两个文件的内容是否匹配。参数f1, f2指定要比较的文件的路径。可选参数shallow指定比较文件时是否需要考虑文件本身的属性(通过os.stat函数可以获得文件属性)。如果文件内容匹配,函数返回True,否则返回False。

filecmp.cmpfiles(dir1, dir2, common[, shallow]):

    比较两个文件夹内指定文件是否相等。参数dir1, dir2指定要比较的文件夹,参数common指定要比较的文件名列表。函数返回包含3个list元素的元组,分别表示匹配、不匹配以及错误的文件列表。错误的文件指的是不存在的文件,或文件被琐定不可读,或没权限读文件,或者由于其他原因访问不了该文件。

    filecmp模块中定义了一个dircmp类,用于比较文件夹,通过该类比较两个文件夹,可以获取一些详细的比较结果(如只在A文件夹存在的文件列表),并支持子文件夹的递归比较。

dircmp提供了三个方法用于报告比较的结果

  • report():只比较指定文件夹中的内容(文件与文件夹)
  • report_partial_closure():比较文件夹及第一级子文件夹的内容
  • report_full_closure():递归比较所有的文件夹的内容

例子:在文件夹"1"中含有文件"1.txt", 在文件夹"2"中含有文件"1.txt"和"2.txt",其两个文件夹下面的文件"1.txt"内容一样,

>>>import filecmp
>>>x = filecmp.dircmp("1", "2")
>>>x.report()
>>>
diff 1 2
Only in 2 : ['2.txt']
Identical files : ['1.txt']

如果两个文件夹下面的文件"1.txt"内容不相同那么结果如下:

>>>import filecmp
>>>x = filecmp.dircmp("1", "2")
>>>x.report()
>>>
diff 1 2
Only in 2 : ['2.txt']
Differing files : ['1.txt'] 

dircmp还提供了下面这些属性用于获取比较的详细结果

  • left_list:左边文件夹中的文件与文件夹列表;
  • right_list:右边文件夹中的文件与文件夹列表;
  • common:两边文件夹中都存在的文件或文件夹;
  • left_only:只在左边文件夹中存在的文件或文件夹;
  • right_only:只在右边文件夹中存在的文件或文件夹;
  • common_dirs:两边文件夹都存在的子文件夹;
  • common_files:两边文件夹都存在的子文件;
  • common_funny:两边文件夹都存在的子文件夹;
  • same_files:匹配的文件;
  • diff_files:不匹配的文件;
  • funny_files:两边文件夹中都存在,但无法比较的文件;
  • subdirs:我没看明白这个属性的意思,python手册中的解释如下:A dictionary mapping names in common_dirs to dircmp objects

    简单就是美!我只要文件比较的结果,不想去关心文件是如何是比较的,hey,就用python吧~~












  



      

你可能感兴趣的:(python3 常用模块(时间、os路径及文件操作、字符串比较,文件内容比较等模块))