1. 程式人生 > >Python常見文件操作的函數示例

Python常見文件操作的函數示例

修改文件權限 pygame SM details 系統 true write 文件 當前

# -*-coding:utf8 -*-  
  
‘‘‘‘‘ 
    Python常見文件操作示例 
 
    os.path 模塊中的路徑名訪問函數 
    分隔 
    basename() 去掉目錄路徑, 返回文件名 
    dirname() 去掉文件名, 返回目錄路徑 
    join() 將分離的各部分組合成一個路徑名 
    split() 返回 (dirname(), basename()) 元組 
    splitdrive() 返回 (drivename, pathname) 元組 
    splitext() 返回 (filename, extension) 元組 
 
    信息 
    getatime() 返回最近訪問時間 
    getctime() 返回文件創建時間 
    getmtime() 返回最近文件修改時間 
    getsize() 返回文件大小(以字節為單位) 
 
    查詢 
    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):規範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.創建多級目錄:os.makedirs(r"c:\python\test") 
 
    7.創建單個目錄:os.mkdir("test") 
 
    8.刪除多個目錄:os.removedirs(r"c:\python") #刪除所給路徑最後一個目錄下所有空目錄。 
 
    9.刪除單個目錄:os.rmdir("test") 
 
    10.獲取文件屬性:os.stat(file) 
 
    11.修改文件權限與時間戳:os.chmod(file) 
 
    12.執行操作系統命令:os.system("dir") 
 
    13.啟動新進程:os.exec(), os.execvp() 
 
    14.在後臺執行程序: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.獲取文件大小:os.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.創建一個唯一的臨時文件:tempfile.mktemp() --> filename 
 
    2.打開臨時文件:tempfile.TemporaryFile() 
 
    內存文件(StringIO和cStringIO)操作 
    [4.StringIO] #cStringIO是StringIO模塊的快速實現模塊 
 
    1.創建內存文件並寫入初始數據:f = StringIO.StringIO("Hello world!") 
 
    2.讀入內存文件數據:print f.read() #或print f.getvalue() --> Hello world! 
 
    3.想內存文件寫入數據: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()  

  

#讀文件的寫法:  
#讀文本文件:   
input = open(‘data‘, ‘r‘)#第二個參數是默認的,可以不加  
#讀二進制文件:   
input = open(‘data‘, ‘rb‘)  
#讀取所有文件內容:  
open(‘xxoo.txt‘).read()  
#讀取固定字節  
open(‘abinfile‘, ‘rb‘).read(100)  
#讀每行  
file_object.readlines()  

  轉自:https://blog.csdn.net/scelong/article/details/6971917

Python常見文件操作的函數示例