Python常見文件操作的函數(shù)示例代碼
更新時間:2011年11月15日 17:07:07 作者:
Python常見文件操作的函數(shù)示例代碼,學習python的朋友可以參考下。
復制代碼 代碼如下:
# -*-coding:utf8 -*-
'''
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.修改文件權(quán)限與時間戳: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.獲取文件大小: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.創(chuàng)建一個唯一的臨時文件:tempfile.mktemp() --> filename
2.打開臨時文件:tempfile.TemporaryFile()
內(nèi)存文件(StringIO和cStringIO)操作
[4.StringIO] #cStringIO是StringIO模塊的快速實現(xiàn)模塊
1.創(chuàng)建內(nèi)存文件并寫入初始數(shù)據(jù):f = StringIO.StringIO("Hello world!")
2.讀入內(nèi)存文件數(shù)據(jù):print f.read() #或print f.getvalue() --> Hello world!
3.想內(nèi)存文件寫入數(shù)據(jù):f.write("Good day!")
4.關(guān)閉內(nèi)存文件: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')#第二個參數(shù)是默認的,可以不加
#讀二進制文件:
input = open('data', 'rb')
#讀取所有文件內(nèi)容:
open('xxoo.txt').read()
#讀取固定字節(jié)
open('abinfile', 'rb').read(100)
#讀每行
file_object.readlines()
相關(guān)文章
Python的Tornado框架實現(xiàn)異步非阻塞訪問數(shù)據(jù)庫的示例
Tornado框架的異步非阻塞特性是其最大的亮點,這里我們將立足于基礎(chǔ)來介紹一種簡單的Python的Tornado框架實現(xiàn)異步非阻塞訪問數(shù)據(jù)庫的示例:2016-06-06
python數(shù)據(jù)分析基礎(chǔ)知識之shape()函數(shù)的使用教程
shape函數(shù)是numpy.core.fromnumeric中的函數(shù),它的功能是讀取矩陣的長度,比如shape[0]就是讀取矩陣第一維度的長度,下面這篇文章主要給大家介紹了關(guān)于python數(shù)據(jù)分析基礎(chǔ)知識之shape()函數(shù)使用的相關(guān)資料,需要的朋友可以參考下2022-09-09
穩(wěn)扎穩(wěn)打?qū)WPython之容器 可迭代對象 迭代器 生成器專題講解
在剛開始學Python的時候,是不是經(jīng)常會聽到大佬們在講容器、可迭代對象、迭代器、生成器、列表/集合/字典推導式等等眾多概念,其實這不是大佬們沒事就擱那扯專業(yè)術(shù)語來裝B,而是這些東西都得要明白的,光知道字符串、列表等基礎(chǔ)還是不夠的,尤其是在Python的數(shù)據(jù)結(jié)構(gòu)方面2021-10-10
python光學仿真實現(xiàn)光線追跡之空間關(guān)系
這篇文章主要介紹了python光學仿真中實現(xiàn)光線追跡的空間關(guān)系示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10

