python 文件與目錄操作
更新時(shí)間:2008年12月24日 23:16:36 作者:
可以使用簡(jiǎn)單的方法匹配某個(gè)目錄下的所有子目錄或文件,用法也很簡(jiǎn)單。
1)os.path
1.1 os.path.isabs(path) 是否是絕對(duì)路徑
1.2 os.path.isfile(path)
1.3 os.path.isdir(path)
1.4 os.path.islink(path) 是否是鏈接;但如果系統(tǒng)不支持鏈接,返回False
1.5 os.path.ismount(path) 是否為驅(qū)動(dòng)器;但是很不幸的是在python 3.0中這是個(gè)不能運(yùn)行的函數(shù)。
原函數(shù)如下:
# Is a path a mount point? Either a root (with or without drive letter)
# or an UNC path with at most a / or \ after the mount point.
def ismount(path):
"""Test whether a path is a mount point (defined as root of drive)"""
unc, rest = splitunc(path)
seps = _get_bothseps(p)
if unc:
return rest in p[:0] + seps
p = splitdrive(path)[1]
return len(p) == 1 and p[0] in seps
其錯(cuò)誤之處是顯而易見(jiàn)的。不知道這個(gè)函數(shù)為什么這么寫,在windows平臺(tái),可以如下完成該功能
def ismount(path):
p = splitdrive(path)[1]
if len(p) > 0:
return(False)
else:
return(True)
其他平臺(tái)沒(méi)有對(duì)應(yīng)的機(jī)器,不知道具體情形。
1.6 os.path.abspath(path) 返回絕對(duì)路徑
1.7 os.path.dirname(path)
1.8 os.path.exists(path)
1.9 os.path.lexists(path) 和exists函數(shù)一樣
1.10os.path.getsize(path)
1.11os.path.getctime(path) 返回浮點(diǎn)數(shù)的系統(tǒng)時(shí)間,在類Unix系統(tǒng)上是文件最近更改的時(shí)間,
在Windows上是文件或目錄的創(chuàng)建時(shí)間
1.12os.path.getmtime(path) 文件或目錄最后更改的時(shí)間
1.13os.path.getatime(path) 文件或目錄最后存取的時(shí)間
1.14os.path.samefile(path1,path2) 如果2個(gè)路徑指向同樣的文件或目錄,返回True(Windows上不可用)
1.15os.path.split(path) 分割路徑,如果path是目錄,返回[parentName, dirName];
如果path是文件,返回[dirName, fileName]
1.16os.path.splitext(path) 分割路徑,如果path是目錄,返回[parentName, ''];
如果path是文件,返回[dirName+fileName, 文件后綴]
2)fileinput
簡(jiǎn)單使用
import file
input for line in fileinput.input():
process(line)
2.1 fileinput.input([files[, inplace[, backup[,mode[,openhook]]]]])
創(chuàng)建一個(gè)fileinput的實(shí)例,如果files為空,則指向控制臺(tái)獲得輸入;如果file為'-',同樣轉(zhuǎn)向控制臺(tái)獲得輸入。
默認(rèn)情況,文件以text mode打開(kāi),如果需要其他格式,則需要指定。
2.2 fileinput.filename() #只有當(dāng)讀入第一行之后,該值才被賦值
2.3 fileinput.fileno()
2.4 fileinput.lineno()
2.5 fileinput.filelineno()
2.6 fileinput.isfirstline()
2.7 fileinput.isstdin()
2.8 fileinput.nextfile()
2.9 fileinput.close()
3)glob
可以使用簡(jiǎn)單的方法匹配某個(gè)目錄下的所有子目錄或文件,用法也很簡(jiǎn)單。
3.1 glob.glob(regression) 返回一個(gè)列表
3.2 glob.iglob(regression) 返回一個(gè)遍歷器
這個(gè)模塊簡(jiǎn)單好用,強(qiáng)力推薦。
4)linecache
看名字就知道了,屬于緩存類的
4.1 linecache.getline(filename,lineno[, module_globals]) #獲得filename的第lineno行
4.2 linecache.clearcache()
4.3 linecache.checkcache([filename]) #檢查更新
5)shutil 重點(diǎn)推薦的襖,好東西,支持文件集合的復(fù)制和刪除操作
5.1 shutil.copyfileobj(fsrc, fdst[, length])
5.2 shutil.copyfile(src, dst) #上面2個(gè)都是文件的復(fù)制
5.3 shutil.copymode(src, dst) #除了復(fù)制內(nèi)容,還會(huì)復(fù)制其他的一些信息,例如作者
5.4 shutil.copystat(src, dst) #除了復(fù)制內(nèi)容,還會(huì)復(fù)制存取時(shí)間的信息
5.5 shutil.copy(src, dst) #復(fù)制文件到dst,當(dāng)dst為目錄時(shí),復(fù)制到子目錄
5.6 shutil.copy2(src, dst) #相當(dāng)于先copy再copystat
5.7 shutil.copytree(src, dst[, symlinks=False[, ingore=None]]) #復(fù)制文件夾樹(shù),注意,dst文件夾必須是不存在的
5.8 shutil.rmtree(path[, ignore_erros[, onerror]])
5.9 shutil.move(src,dst)
def copytree(src, dst, symlinks=False):
names = os.listdir(src)
os.makedirs(dst)
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks)
else:
copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error) as why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error as err:
errors.extend(err.args[0])
try:
copystat(src, dst)
except WindowsError:
# can't copy file access times on Windows
pass
except OSError as why:
errors.extend((src, dst, str(why)))
if errors:
raise Error(errors)
1.1 os.path.isabs(path) 是否是絕對(duì)路徑
1.2 os.path.isfile(path)
1.3 os.path.isdir(path)
1.4 os.path.islink(path) 是否是鏈接;但如果系統(tǒng)不支持鏈接,返回False
1.5 os.path.ismount(path) 是否為驅(qū)動(dòng)器;但是很不幸的是在python 3.0中這是個(gè)不能運(yùn)行的函數(shù)。
原函數(shù)如下:
# Is a path a mount point? Either a root (with or without drive letter)
# or an UNC path with at most a / or \ after the mount point.
def ismount(path):
"""Test whether a path is a mount point (defined as root of drive)"""
unc, rest = splitunc(path)
seps = _get_bothseps(p)
if unc:
return rest in p[:0] + seps
p = splitdrive(path)[1]
return len(p) == 1 and p[0] in seps
其錯(cuò)誤之處是顯而易見(jiàn)的。不知道這個(gè)函數(shù)為什么這么寫,在windows平臺(tái),可以如下完成該功能
def ismount(path):
p = splitdrive(path)[1]
if len(p) > 0:
return(False)
else:
return(True)
其他平臺(tái)沒(méi)有對(duì)應(yīng)的機(jī)器,不知道具體情形。
1.6 os.path.abspath(path) 返回絕對(duì)路徑
1.7 os.path.dirname(path)
1.8 os.path.exists(path)
1.9 os.path.lexists(path) 和exists函數(shù)一樣
1.10os.path.getsize(path)
1.11os.path.getctime(path) 返回浮點(diǎn)數(shù)的系統(tǒng)時(shí)間,在類Unix系統(tǒng)上是文件最近更改的時(shí)間,
在Windows上是文件或目錄的創(chuàng)建時(shí)間
1.12os.path.getmtime(path) 文件或目錄最后更改的時(shí)間
1.13os.path.getatime(path) 文件或目錄最后存取的時(shí)間
1.14os.path.samefile(path1,path2) 如果2個(gè)路徑指向同樣的文件或目錄,返回True(Windows上不可用)
1.15os.path.split(path) 分割路徑,如果path是目錄,返回[parentName, dirName];
如果path是文件,返回[dirName, fileName]
1.16os.path.splitext(path) 分割路徑,如果path是目錄,返回[parentName, ''];
如果path是文件,返回[dirName+fileName, 文件后綴]
2)fileinput
簡(jiǎn)單使用
import file
input for line in fileinput.input():
process(line)
2.1 fileinput.input([files[, inplace[, backup[,mode[,openhook]]]]])
創(chuàng)建一個(gè)fileinput的實(shí)例,如果files為空,則指向控制臺(tái)獲得輸入;如果file為'-',同樣轉(zhuǎn)向控制臺(tái)獲得輸入。
默認(rèn)情況,文件以text mode打開(kāi),如果需要其他格式,則需要指定。
2.2 fileinput.filename() #只有當(dāng)讀入第一行之后,該值才被賦值
2.3 fileinput.fileno()
2.4 fileinput.lineno()
2.5 fileinput.filelineno()
2.6 fileinput.isfirstline()
2.7 fileinput.isstdin()
2.8 fileinput.nextfile()
2.9 fileinput.close()
3)glob
可以使用簡(jiǎn)單的方法匹配某個(gè)目錄下的所有子目錄或文件,用法也很簡(jiǎn)單。
3.1 glob.glob(regression) 返回一個(gè)列表
3.2 glob.iglob(regression) 返回一個(gè)遍歷器
這個(gè)模塊簡(jiǎn)單好用,強(qiáng)力推薦。
4)linecache
看名字就知道了,屬于緩存類的
4.1 linecache.getline(filename,lineno[, module_globals]) #獲得filename的第lineno行
4.2 linecache.clearcache()
4.3 linecache.checkcache([filename]) #檢查更新
5)shutil 重點(diǎn)推薦的襖,好東西,支持文件集合的復(fù)制和刪除操作
5.1 shutil.copyfileobj(fsrc, fdst[, length])
5.2 shutil.copyfile(src, dst) #上面2個(gè)都是文件的復(fù)制
5.3 shutil.copymode(src, dst) #除了復(fù)制內(nèi)容,還會(huì)復(fù)制其他的一些信息,例如作者
5.4 shutil.copystat(src, dst) #除了復(fù)制內(nèi)容,還會(huì)復(fù)制存取時(shí)間的信息
5.5 shutil.copy(src, dst) #復(fù)制文件到dst,當(dāng)dst為目錄時(shí),復(fù)制到子目錄
5.6 shutil.copy2(src, dst) #相當(dāng)于先copy再copystat
5.7 shutil.copytree(src, dst[, symlinks=False[, ingore=None]]) #復(fù)制文件夾樹(shù),注意,dst文件夾必須是不存在的
5.8 shutil.rmtree(path[, ignore_erros[, onerror]])
5.9 shutil.move(src,dst)
復(fù)制代碼 代碼如下:
def copytree(src, dst, symlinks=False):
names = os.listdir(src)
os.makedirs(dst)
errors = []
for name in names:
srcname = os.path.join(src, name)
dstname = os.path.join(dst, name)
try:
if symlinks and os.path.islink(srcname):
linkto = os.readlink(srcname)
os.symlink(linkto, dstname)
elif os.path.isdir(srcname):
copytree(srcname, dstname, symlinks)
else:
copy2(srcname, dstname)
# XXX What about devices, sockets etc.?
except (IOError, os.error) as why:
errors.append((srcname, dstname, str(why)))
# catch the Error from the recursive copytree so that we can
# continue with other files
except Error as err:
errors.extend(err.args[0])
try:
copystat(src, dst)
except WindowsError:
# can't copy file access times on Windows
pass
except OSError as why:
errors.extend((src, dst, str(why)))
if errors:
raise Error(errors)
相關(guān)文章
Python利用xlwt/openpyxl/xlutils實(shí)現(xiàn)寫入Excel數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了Python如何利用xlwt/openpyxl/xlutils這些第三方庫(kù)實(shí)現(xiàn)寫入Excel數(shù)據(jù)功能,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11Jupyter Notebook折疊輸出的內(nèi)容實(shí)例
這篇文章主要介紹了Jupyter Notebook折疊輸出的內(nèi)容實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python中string模塊各屬性以及函數(shù)的用法介紹
下面小編就為大家?guī)?lái)一篇python中string模塊各屬性以及函數(shù)的用法介紹。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-05-05python壓縮文件夾內(nèi)所有文件為zip文件的方法
這篇文章主要介紹了python壓縮文件夾內(nèi)所有文件為zip文件的方法,可實(shí)現(xiàn)簡(jiǎn)單的zip文件壓縮功能,需要的朋友可以參考下2015-06-06python實(shí)現(xiàn)宿舍管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)宿舍管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11Django項(xiàng)目創(chuàng)建的圖文教程
本文主要介紹了Django項(xiàng)目創(chuàng)建的圖文教程,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Python時(shí)間序列處理之ARIMA模型的使用講解
今天小編就為大家分享一篇關(guān)于Python時(shí)間序列處理之ARIMA模型的使用講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-04-04