Python常見文件操作的函數(shù)示例
''''' Python常見文件操作示例
os.path 模塊中的路徑名訪問函數(shù)分隔basename() 去掉目錄路徑, 返回文件名dirname() 去掉文件名, 返回目錄路徑join() 將分離的各部分組合成一個路徑名split() 返回 (dirname(), basename()) 元組splitdrive() 返回 (drivename, pathname) 元組splitext() 返回 (filename, extension) 元組
信息getatime() 返回最近訪問時間getctime() 返回文件創(chuàng)建時間getmtime() 返回最近文件修改時間getsize() 返回文件大。ㄒ宰止(jié)為單位)
查詢exists() 指定路徑(文件或目錄)是否存在isabs() 指定路徑是否為絕對路徑isdir() 指定路徑是否存在且為一個目錄isfile() 指定路徑是否存在且為一個文件islink() 指定路徑是否存在且為一個符號鏈接ismount() 指定路徑是否存在且為一個掛載點samefile() 兩個路徑名是否指向同個文件
os.path.isdir(name):判斷name是不是一個目錄,name不是目錄就返回false os.path.isfile(name):判斷name是不是一個文件,不存在name也返回false os.path.exists(name):判斷是否存在文件或目錄name os.path.getsize(name):獲得文件大小,如果name是目錄返回0L os.path.abspath(name):獲得絕對路徑os.path.normpath(path):規(guī)范path字符串形式os.path.split(name):分割文件名與目錄(事實上,如果你完全使用目錄,它也會將最后一個目錄作為文件名而分離,同時它不會判斷文件或目錄是否存在)
os.path.splitext():分離文件名與擴展名os.path.join(path,name):連接目錄與文件名或目錄os.path.basename(path):返回文件名os.path.dirname(path):返回文件路徑
os模塊中的文件操作:os 模塊屬性linesep 用于在文件中分隔行的字符串sep 用來分隔文件路徑名的字符串pathsep 用于分隔文件路徑的字符串curdir 當前工作目錄的字符串名稱pardir (當前工作目錄的)父目錄字符串名稱
1.重命名:os.rename(old, new)
2.刪除:os.remove(file)
3.列出目錄下的文件:os.listdir(path)
4.獲取當前工作目錄:os.getcwd()
5.改變工作目錄:os.chdir(newdir)
6.創(chuàng)建多級目錄:os.makedirs(r"c:\python\test")
7.創(chuàng)建單個目錄:os.mkdir("test")
8.刪除多個目錄:os.removedirs(r"c:\python") #刪除所給路徑最后一個目錄下所有空目錄。
9.刪除單個目錄:os.rmdir("test")
10.獲取文件屬性:os.stat(file)
11.修改文件權限與時間戳:os.chmod(file)
12.執(zhí)行操作系統(tǒng)命令:os.system("dir")
13.啟動新進程:os.exec(), os.execvp()
14.在后臺執(zhí)行程序:osspawnv()
15.終止當前進程:os.exit(), os._exit()
16.分離文件名:os.path.split(r"c:\python\hello.py") ——> ("c:\\python", "hello.py")
17.分離擴展名:os.path.splitext(r"c:\python\hello.py") ——> ("c:\\python\\hello", ".py")
18.獲取路徑名:os.path.dirname(r"c:\python\hello.py") ——> "c:\\python"
19.獲取文件名:os.path.basename(r"r:\python\hello.py") ——> "hello.py"
20.判斷文件是否存在:os.path.exists(r"c:\python\hello.py") ——> True
21.判斷是否是絕對路徑:os.path.isabs(r".\python\") ——> False
22.判斷是否是目錄:os.path.isdir(r"c:\python") ——> True
23.判斷是否是文件:os.path.isfile(r"c:\python\hello.py") ——> True
24.判斷是否是鏈接文件:os.path.islink(r"c:\python\hello.py") ——> False
25.獲取文件大。簅s.path.getsize(filename)
26.*******:os.ismount("c:\\") ——> True
27.搜索目錄下的所有文件:os.path.walk()
shutil模塊對文件的操作:1.復制單個文件:shultil.copy(oldfile, newfle)
2.復制整個目錄樹:shultil.copytree(r".\setup", r".\backup")
3.刪除整個目錄樹:shultil.rmtree(r".\backup")
臨時文件的操作:1.創(chuàng)建一個唯一的臨時文件:tempfile.mktemp() ——> filename
2.打開臨時文件:tempfile.TemporaryFile()
內存文件(StringIO和cStringIO)操作[4.StringIO] #cStringIO是StringIO模塊的快速實現(xiàn)模塊
1.創(chuàng)建內存文件并寫入初始數(shù)據(jù):f = StringIO.StringIO("Hello world!")
2.讀入內存文件數(shù)據(jù):print f.read() #或print f.getvalue() ——> Hello world!
3.想內存文件寫入數(shù)據(jù):f.write("Good day!")
4.關閉內存文件:f.close()
''' import os import os.path import unittest import time #import pygame
class PyFileCommonOperatorTest(unittest.TestCase):def __init__(self):"""constructor"""
def test01(self):print os.linesep print os.sep print os.pathsep print os.curdir print os.pardir print os.getcwd()
print 'unittest here'
if __name__ == "__main__":t = PyFileCommonOperatorTest()
t.test01()
view plainprint?
view plainprint?
#讀文件的寫法:#讀文本文件:input = open('data', 'r')#第二個參數(shù)是默認的,可以不加#讀二進制文件:input = open('data', 'rb')
#讀取所有文件內容:open('xxoo.txt')。read()
#讀取固定字節(jié)open('abinfile', 'rb')。read(100)
#讀每行file_object.readlines()